mac使用C语言open函数的相对路径
在用mac学习C语言fopen函数时,对路径问题产生了一点疑惑。
#include <stdio.h>
int main()
{
FILE *fp = NULL;
fp = fopen("/tmp/test.txt", "w+");
fprintf(fp, "This is testing for fprintf...\n");
fputs("This is testing for fputs...\n", fp);
fclose(fp);
}
这里的相对路径是你.c文件开始的径路,就是你.c文件在哪儿,这个相对路径就从哪儿开始。
然后这段代码需要做一点修改,把第一个/去掉,不然就算运行成功了也不能向文件中写入东西,可能这就是macos跟windows系统的不同。
修改后的代码:
#include <stdio.h>
int main()
{
FILE *fp = NULL;
fp = fopen("tmp/test.txt", "w+");
fprintf(fp, "This is testing for fprintf...\n");
fputs("This is testing for fputs...\n", fp);
fclose(fp);
}