我有一个打开 .txt 的函数,使用 fscanf 读取格式如下的数字:
532
2
-234
32
当我使用 GCC 编译时它成功地做到了这一点,但我无法在 Xcode 中打开文件,为什么?
相关代码是:
int main (void){
FILE *infile = NULL; //input file buffer
int int_array[100]; //an integer array 100 entries long
char infilename[256]; //an extra large char array
//this entire while loop was provided as part of the assignment,
//we weren't supposed to change it. I've finished this assignment,
//but now I want to know how to use my Xcode debugger
while(infile == NULL) {
printf("Input filename:"); //user prompt
scanf("%s",infilename); //store user input in large char array
if((infile = fopen(infilename, "r")) == NULL) { //input file buffer opens file
printf("ERROR: file %s can not be opened!\n",in filename); //error if can not open
}
}
int arraySize =0;
while (!feof(readfile)) { //StackOverflow showed me how to use this function.
fscanf(readfile, "%d", (array+arraySize));
arraySize++;
}
//more code...
}
在我的 Xcode 项目中,hw1.c、hw1_functions.c、input.txt 和一些未调用的 c 函数都位于同一文件夹中。当我运行该程序时,它会提示我输入文件名。我说 input.txt 就像使用 GCC 后在终端中所做的一样,但我得到“错误文件 input.txt 无法打开!”
我需要做些什么才能使这项工作成功吗?
确保 input.txt 文件不在项目文件夹中,而是与编译的程序位于同一文件夹中。您可以通过右键选择 Xcode 左窗格中“Products”文件夹下的可执行文件并选择“在查找器中显示”来找到此文件,然后将 .txt 文件移至此处,然后再次运行它。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)