我的C程序读取一个文本文件.当我将其放入Xcode时,它会失败.发生了什么? [英] My C program reads a text file. When I put it into Xcode it fails. What's happening?

查看:73
本文介绍了我的C程序读取一个文本文件.当我将其放入Xcode时,它会失败.发生了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个打开.txt的函数,使用fscanf读取格式如下的数字:

I have a function that opens a .txt, uses fscanf to read numbers formatted like:

532
2
-234
32

等当我使用GCC进行编译时,它成功完成了此操作,但是我无法在Xcode中打开文件,为什么? 相关代码为:

etc. It successfully does this when I compile using GCC, but I can't get the file to open in Xcode, why? The relevant code is:

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函数.当我运行程序时,它提示我输入文件名.我说输入.txt就像在使用GCC之后在终端中一样,但是我得到错误文件input.txt无法打开!" 我需要做些什么才能完成这项工作吗?

In my Xcode project I have hw1.c, hw1_functions.c, input.txt, and some uncalled c functions all in the same folder. When I run the program, it prompts me to enter the name of the file. I say input.txt just like I do in the terminal after using GCC, but I get "ERROR file input.txt can not be opened!" Is there something I need to do to make this work?

推荐答案

确保input.txt文件不在项目文件夹中,而是与已编译程序位于同一文件夹中.您可以通过右键选择Xcode左窗格中产品"文件夹下的可执行文件,然后选择在查找程序中显示",然后将您的.txt文件移到此处,然后再次运行它来找到它.

Make sure that the input.txt file is not in the project folders, but in the same folder as the compiled program. You can find this by right selecting the executable under the 'Products' folder in the left pane of Xcode and selecting "Show in finder" and then move your .txt file there, and run it again.

这篇关于我的C程序读取一个文本文件.当我将其放入Xcode时,它会失败.发生了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆