1

I'm trying to get my xcode to read in a file, but I keep getting a "build succeeded" and then (11db) as the output.

I have saved a file called "sudokode.in" to my desktop, and that's what I'm trying to open.

The file only has an integer, 19, in it.

I just want to print out 19 to the screen.

I have never gotten my Xcode to read a file before, so I wouldn't know if I have to set it up, or what. I have searched online and haven't found a real solution to this problem.

I appreciate the help.

int main() {

int num;

FILE* ifp = fopen("sudokode.in", "r");
fscanf(ifp, "%d", &num);

printf("%d", num);

return 0;
}
4

2 回答 2

4

该文件可能不存在。如果是这种情况,ifp将会是NULL,所以检查一下:

int main() {

int num;

FILE* ifp = fopen("sudokode.in", "r");
if (ifp == NULL) {
    printf("Oops, this file doesn't exist!\n");
    return -1;
}
fscanf(ifp, "%d", &num);

printf("%d", num);

return 0;
}

您的程序仅在您从存储的同一目录运行时才有效sudoke.in。您可以改用绝对路径(例如/User/John/Desktop/sudoke.in)。

于 2013-03-02T16:28:26.043 回答
0

要设置您的工作目录以在 Xcode 中读取和写入文件,请转到 Product --> Scheme --> Edit Scheme,然后单击它。

然后,单击“工作目录”旁边的复选框,并使用下面字段末尾的灰色小文件图标来设置工作目录。

我在这里找到了关于如何在 Xcode 上的 c 中为基本命令行项目执行此操作的很好的解释:

https://www.meandmark.com/blog/2013/12/setting-the-current-working-directory-for-xcode-command-line-projects/

于 2020-09-18T05:08:37.497 回答