gpt4 book ai didi

C-opendir + fopen

转载 作者:太空宇宙 更新时间:2023-11-04 05:35:08 30 4
gpt4 key购买 nike

我想知道是否可以打开一个目录,然后使用 fopen 从中打开一个文件。就像在示例中一样:

我有一个 MAINDIR,里面有 file.txt,所以我想执行以下操作:

void openFile(const char * filename)
{
opendir(MAINDIR);
fopen(filename, "r");
}

int main()
{
openFile("file.txt");
return 0;
}

我知道我可以这样做:fopen("path/to/main/dir/filename.txt", "r") 但我想要更通用的东西。打开一个目录,然后使每个 fopen 在该目录中工作

谢谢

最佳答案

您可以使用 chdir(const char *) 更改您的工作目录,例如:

chdir("/your/path/")

关于C-opendir + fopen,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40977719/

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