C语言 我的 C 程序读取一个文本文件。当我将它放入 Xcode 时,它​​失败了。发生了什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25877635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:23:28  来源:igfitidea点击:

My C program reads a text file. When I put it into Xcode it fails. What's happening?

cxcodefopen

提问by Lost Odinson

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

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

532
2
-234
32

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:

等等。当我使用 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...
}

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?

在我的 Xcode 项目中,我有 hw1.c、hw1_functions.c、input.txt 和一些未调用的 c 函数都在同一个文件夹中。当我运行程序时,它提示我输入文件名。我说 input.txt 就像我在使用 GCC 后在终端中所做的那样,但我得到“错误文件 input.txt 无法打开!” 我需要做些什么才能使这项工作发挥作用?

回答by Xavier Dass

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.

确保 input.txt 文件不在项目文件夹中,而是在与编译程序相同的文件夹中。您可以通过右键选择 Xcode 左窗格中“Products”文件夹下的可执行文件并选择“Show in finder”,然后将您的 .txt 文件移到那里,并再次运行它来找到它。

example image

示例图像

回答by joliejuly

I've struggled with the same problem today. I wanted to add C code to my Swift project and my file pointer was always NULL.

我今天一直在努力解决同样的问题。我想将 C 代码添加到我的 Swift 项目中,而我的文件指针始终为 NULL。

Unfortunately, in XCode 9 for iOS app, I couldn't change the working directory. Changing Build phases didn't help me either. After 4+ hours of trial and error, that's what I've come up with finally and it works:

不幸的是,在 iOS 应用程序的 XCode 9 中,我无法更改工作目录。更改构建阶段也没有帮助我。经过 4 个多小时的反复试验,这就是我最终想出的,并且有效:

  1. when copying files to XCode, I've chosen "Create groups", but I needed to choose "Create folder references":
  1. 将文件复制到 XCode 时,我选择了“创建组”,但我需要选择“创建文件夹引用”:

enter image description here

在此处输入图片说明

  1. I created a new objective-c file (.m) and copied all my C code there.

  2. I left untouched .h files (XCode generated bridging header and my own .h file with public functions declaration). Now my project structure looked like this:

  1. 我创建了一个新的 Objective-c 文件 (.m) 并将我所有的 C 代码复制到那里。

  2. 我没有改动 .h 文件(XCode 生成的桥接头和我自己的带有公共函数声明的 .h 文件)。现在我的项目结构是这样的:

enter image description here

在此处输入图片说明

  1. In my dict.m file in place of previous plain c fopenpart:

    FILE *dic = fopen("dictionary.txt", "r");
    

    I added obj-C code:

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"dictionary" ofType:@"txt"];
    FILE *dic = fopen([filePath cStringUsingEncoding: NSUTF8StringEncoding], "r");
    
  1. 在我的 dict.m 文件中代替之前的普通 cfopen部分:

    FILE *dic = fopen("dictionary.txt", "r");
    

    我添加了 obj-C 代码:

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"dictionary" ofType:@"txt"];
    FILE *dic = fopen([filePath cStringUsingEncoding: NSUTF8StringEncoding], "r");
    

And it works now without any problem! It's just amazing!

它现在可以正常工作了!真是太棒了!

ps I decided to write this answer in case it will help someone like me and will save them some time. If you know how to change working directory in XCode 9 for iOS, please, leave me a comment - now I am really curious why I can't find it.

ps我决定写这个答案,以防它对像我这样的人有所帮助并节省他们一些时间。如果您知道如何在 iOS 的 XCode 9 中更改工作目录,请给我留言 - 现在我真的很好奇为什么我找不到它。