Resolved C: Can't open file using fopen

Discussion in 'Mac Programming' started by jsmwoolf, Oct 3, 2011.

  1. jsmwoolf, Oct 3, 2011
    Last edited: Oct 3, 2011

    jsmwoolf macrumors regular

    Joined:
    Aug 17, 2011
    #1
    I've ran into this problem before while learning C, but I'm trying to solve a problem on Project Euler(no surprise) that requires the user to open a file and read data off. I haven't wrote an algorithm on how to solve it, but the problem is that somehow, I can't open a file using fopen or write to a file. So far I have this code to simply try to open the file:

    Code:
    #include <stdio.h>
    
    int main (int argc, const char * argv[])
    {
        FILE *list;
        list = fopen("roman.txt", "r");
        if(list==NULL)
        {
            printf("Error! Can't find file!\n");
            return 1;
        }
    }
    
    I tried to even add the txt file to the workspace, but to no success. The file is with the directory and I'm writing the actual project on a USB drive. The question is what's going on and why doesn't it work?
     
  2. naples98 macrumors member

    naples98

    Joined:
    Sep 9, 2008
    Location:
    Houston
    #2
    Are you getting an error message? If so, what is it? Does the code compile? Do you get the "Error! Can't find file!" message?
     
  3. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #3
    You need to add the file to the directory where your executable file is, or better provide a path to the file in the first argument.
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    You need to find out what the actual working directory is when your program runs.

    This code uses a relative pathname:
    Code:
        list = fopen("roman.txt", "r");
    
    "Relative" means "relative to the working directory". So find out what the working directory actually is, and work things out from there. By that, I mean either put the file in the right working directory, or figure out how to change the working directory when your program runs. Since you haven't said exactly how you're running your program, nor exactly where the file resides relative to your running program, it's difficult to be more precise with only the information you've posted.


    To find out what the working directory is, use the getcwd() C function. It has a man page, which should be readable in Xcode (look under its Help menu).

    It's definitely readable in Terminal (man getcwd) and on the web:
    http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/getwd.3.html
     
  5. jsmwoolf thread starter macrumors regular

    Joined:
    Aug 17, 2011
    #5
    Oh, I guess I wasn't specific. Yes, I do get the printf prompt when executing it. I have actually found the directory, placed it, and it now works, but how can I change the directory to the project folder?
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    What's your development environment, Xcode? Which version of Xcode?
     
  7. jsmwoolf thread starter macrumors regular

    Joined:
    Aug 17, 2011
  8. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #8
    Edit your scheme. Example picture:

    [​IMG]

    Notice the "Working Directory" option, and the "Use custom working directory" checkbox. Change that.

    If you don't know how to edit schemes, you should probably spend a little time now learning how to configure and manage schemes. It's likely to come up repeatedly for any non-trivial use of Xcode 4.

    There are online reference docs for Xcode and schemes. I'm pretty sure Xcode still has a Help menu, too.
     
  9. jsmwoolf thread starter macrumors regular

    Joined:
    Aug 17, 2011
  10. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #10

Share This Page