gpt4 book ai didi

C 从控制台打开文件

转载 作者:行者123 更新时间:2023-11-30 16:47:57 25 4
gpt4 key购买 nike

我有一个简单的代码,以读取模式从硬盘打开一个文件,但是代码中已经给出了文件的路径。我希望用户通过控制台手动提供文件名、路径和文件打开类型。我必须为此做什么?

#include<stdio.h> 

int main() {
FILE *fp;
fp = fopen("D:\\samplefile.txt","r");
if(fp != NULL) {
printf("File has been opened");
fclose(fp);
}
else printf("File not found");
return 0;
}

最佳答案

// Use of function scanf is done here as an simple use. There are other ways also to get input from user
#include<stdio.h>
int main()
{
FILE *fp;
char filePath[100];
printf("Enter Path name: ");
scanf("%s", filePath);
printf("value is %s",filePath);
fp = fopen(filePath,"r");
if(fp != NULL) {
printf("File has been opened");
fclose(fp);
}
else printf("File not found");
return 0;
}

关于C 从控制台打开文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43163931/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com