Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Xcode中用C执行文件I/O?_C_File Io - Fatal编程技术网

如何在Xcode中用C执行文件I/O?

如何在Xcode中用C执行文件I/O?,c,file-io,C,File Io,我使用Xcode作为C的IDE #include <stdio.h> int main (int argc, const char * argv[]) { FILE *file; int numberTest = 0; file = fopen("fileTest.dat", "r"); if (file == NULL) { printf("File not found!\n"); } fscanf(file

我使用Xcode作为C的IDE

#include <stdio.h>

int main (int argc, const char * argv[])
{
    FILE *file;
    int numberTest = 0;
    file = fopen("fileTest.dat", "r");
    if (file == NULL)
    {
        printf("File not found!\n");
    }
    fscanf(file, "%d", &numberTest);
    printf("I read the number: %d\n", numberTest);
    fclose(file);
    return 0;
}
#包括
int main(int argc,const char*argv[]
{
文件*文件;
int numberTest=0;
file=fopen(“fileTest.dat”,“r”);
if(file==NULL)
{
printf(“未找到文件!\n”);
}
fscanf(文件“%d”和编号测试);
printf(“我读取了号码:%d\n”,numberTest);
fclose(文件);
返回0;
}

该文件位于我的Xcode捆绑包中,与HDD中项目的其余部分位于同一文件夹中。问题是fopen()一直返回NULL。

由于运行可执行文件时文件不在您的路径上,因此找不到该文件。您需要将文件添加到目标中,以便在生成源代码时将其复制到生成目录。

您假设启动可执行文件时,该文件位于当前工作目录中,但可能不是,而且这始终是一个错误的假设,即使在某些情况下恰好是这样

作为一个快速解决方案,您现在只需硬编码文件的绝对路径,例如

file = fopen("/Users/me/Desktop/fileTest.dat", "r");

fopen(“~/Users/fernan/SoftDev/C/CTesting/CTesting/fileTest.dat”、“r”)
我从fileTest.dat获得了路径,fopen()仍然返回NULL。在这样的路径中不能使用
~
,只有shell知道如何展开
~
。另外,路径也是错误的-我很确定您没有路径
~/Users/fernan/SoftDev/…
-您可能是指
~fernan/SoftDev/…
~/SoftDev/…