【发布时间】:2014-11-10 17:25:46
【问题描述】:
我有一个打开 .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 无法打开!” 我需要做些什么来完成这项工作吗?
【问题讨论】:
-
也许验证您当前的工作目录?
-
我在下面的回答有帮助吗?
-
@XavierDass 是的!年轻的笨蛋我不太擅长SO。我的道歉。