3

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

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:

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?

D A Vincent
  • 364
  • 2
  • 21
Lost Odinson
  • 418
  • 1
  • 5
  • 14

2 Answers2

26

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.

example image

Xavier Dass
  • 575
  • 1
  • 9
  • 25
1

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.

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:

  1. when copying files to XCode, I've chosen "Create groups", but I needed to choose "Create folder references":

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:

enter image description here

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

    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");
    

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.

joliejuly
  • 2,127
  • 1
  • 21
  • 24