Cant read text input in xCode 4? C language

Discussion in 'Mac Programming' started by Angelus Mortis, Dec 2, 2011.

  1. Angelus Mortis, Dec 2, 2011
    Last edited by a moderator: Dec 2, 2011

    macrumors newbie

    Joined:
    Dec 2, 2011
    #1
    I use Xcode4, and my program won't seem to open a text input file. I tried everything from renaming the file name to "sample.txt" and "sample" only, but it still doesn't work.
    I also tried putting it in all directories.

    It worked in windows Dev C++ BLood shed, but doesn't seem to work in Xcode4.


    Please help, machine project is due soon.
    P.S. Begginner programmer here, so please don't use too technical terms, I would appreciate a step by step. Thanks.

    Nothing wrong in my code I'm sure, but here you go.

    Heres my code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main() {
    char filename[40];
    char ch;
    FILE *fp;
    
    fprintf(stdout, "Input filename: "); 
    fscanf(stdin, "%s", filename);
    
    if ((fp = fopen(filename, "r")) == NULL) 
    {
        fprintf(stderr, "ERROR: %s does not exist.\n", filename); exit(1);
    }
    while ((fscanf(fp, "%c", &ch)) == 1) fprintf(stdout, "%c", ch); 
    
    fclose(fp);
    return 0; }
    
     
  2. macrumors 68040

    Joined:
    Feb 2, 2008
    #2
    Change your working directory (don't know what Xcode defaults to) in Product > edit scheme > info

    or use an absolute path.
     
  3. thread starter macrumors newbie

    Joined:
    Dec 2, 2011
    #3
    Sorry but how do you change your working directory?
    I went to Product > edit scheme > info
    but it just says
    Build configuration : Debug
    Executable : Filename
    Debugger: GDB
     
  4. macrumors 68040

    Joined:
    Feb 2, 2008
    #4
    But why don't you just use an absolute path, it's just to test the functionality right. You could as well run the executable in the terminal, then you would be working in home directory by default. And perhaps use the terminal to compile as well, then you don't have to learn Xcode as well as C at the same time, just a thought.
     

    Attached Files:

  5. thread starter macrumors newbie

    Joined:
    Dec 2, 2011
    #5
    OMG Thanks so much. haha.
    Wish me luck in this field! :D
     

Share This Page