如何将输出文件保存到 c 中它所属的文件夹中

问题描述 投票:0回答:1

该文件当前保存到 /Users/benreaby,而我希望它保存到可执行文件的源文件夹

#define FILENAME "TACmFile.txt" // Filename that I want to save to the source folder that the executable is located in

int main(int argc, const char * argv[]) {
    
      fp = fopen(FILENAME, "w");
    
      fprintf(fp, "hello world");

      fclose(fp);
    
    return 0;
}
c file directory
1个回答
0
投票

当运行可执行文件并使用相对文件路径时,文件 TACmFile.txt 将写入可执行文件运行时当前工作目录中的位置。这不一定与源代码所在的目录有任何相似之处。

如果你想覆盖它,你需要指定一个绝对路径。您可能需要从配置文件中读取可执行文件以获取文件路径信息。

© www.soinside.com 2019 - 2024. All rights reserved.