Help with C programming: inputting files into code

Discussion in 'Mac Programming' started by thabandit, Mar 5, 2009.

  1. thabandit macrumors member

    Nov 10, 2007
    Hey all

    I do a bit of c programming for school and tried out xcode, played around a bit and finally figured stuff out, wrote my code, ran it, it works fine.

    However, when i try to load a file to my code, it wont load in xcode....i have complied it in terminal and works fine

    the file is called data.txt and IS in the same directory (remember it works fine with terminal)

    g++ filename -o programname, it works fine....

    anyone know how to load the file into xcode, i have tried my best...please check out the screenshot as well....

    Attached Files:

  2. kainjow Moderator emeritus


    Jun 15, 2000
    Xcode sets the current working directory differently. By default it is the build directory, so unless your file is in that folder, it won't work. If you go to the Executables group, then double-click your target's executable you can change the working directory to something else. Or just use absolute paths.
  3. thabandit thread starter macrumors member

    Nov 10, 2007
    ^ wow thanks, that worked perfect double clicked on executables and under general chose project directory instead of build products directory. can u explain the absolute path a bit more?
  4. LtRammstein macrumors 6502a


    Jun 20, 2006
    Denver, CO
    What he means by absolute paths is that there is a designated path in which the file uses.

    So your User's directory has this absolute path:


    So what you can do instead of copying the file into your directory, you can just type in the path. I forget how Xcode reads the path of the file...

    So in your C program where you define the file name you can do this instead:

    file = "~/Documents/ThisIsTheFileIamWorkingWith.txt";
    Or something of the sort.
  5. thabandit thread starter macrumors member

    Nov 10, 2007
    ^oh that's absolute paths...I should have known that, thanks for clearing it up....makes a lot more sense.

    Thanks for your input!!!!

Share This Page