gpt4 book ai didi

c - fopen() 搜索路径

转载 作者:行者123 更新时间:2023-11-30 14:27:09 25 4
gpt4 key购买 nike

有没有办法为 fopen() 设置搜索路径,以便当我输入文件名时,它会在这些路径中搜索该文件?

f=fopen("xxx","r");

我只是想让xxx不在当前目录下,并且不想更改源代码。

最佳答案

不,fopen 只查找当前目录,你不能给它一个目录列表。但如果您希望它搜索路径列表:

char *paths[] = {
".",
"/etc",
"/usr/local",
NULL
};

FILE *fp = NULL;
char path[ENOUGH];

while (!fp && paths[i]) {
sprintf(path, "%s/%s", paths[i], name);
fp = fopen(path, "r");

i++;
}

关于c - fopen() 搜索路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8147330/

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