gpt4 book ai didi

c - 要打开的文件的目录

转载 作者:太空宇宙 更新时间:2023-11-04 07:20:26 26 4
gpt4 key购买 nike

int main()
{
FILE *fp;
fp=fopen("pr1.c","r");
if(fp==NULL)
{
printf("can not open file");
}
getch();
return 0;
}

在上面的代码中,文件pr1.c应该在我工作的同一目录下,否则它会打印can not open file
有什么方法可以打开我计算机中任何位置的文件?

最佳答案

是的,你可以。只需使用文件的完整路径,或基于当前文件夹的相对路径。

例如:

fp = fopen("c:\\MyDirectory\\TestFile1.txt", "r");
fp = fopen("/full/path/to/TestFile1.txt", "r"); // use ../ go to parent folder

更新:如果你想让用户输入路径,你可以使用

char path[201];
scanf("%200s",path); // read from user
fp = fopen(path, "r");

关于c - 要打开的文件的目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21952211/

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