Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Problem with fopen in xcode (C)

  1. Sep 17, 2008 #1
    Hello all, I'm having a very frustrating time with this piece of code. For some reason, I can't seem to open the file I'm trying to read data in from. This piece of code works in everything I've tried, except xCode. This wouldn't bother me, but I really only use my laptop for coding, so I need it to work in xCode.

    The debugger stops at the fopen command line, like it can't find the file.

    If any of you can give me any help I would really appreciate it. I've included the original code and a screenshot of the XCode setup.

  2. jcsd
  3. Sep 17, 2008 #2


    User Avatar
    Science Advisor
    Homework Helper

    It stopped because you set a breakpoint?
    Press f5 (or whatever in xcode) or clear the breakpoint.
  4. Sep 17, 2008 #3
    Thanks for that advice. That fixed one problem, but brought up another...

    When compiling I get a "signal 10 (SIGBUS)" error. I looked it up and according to "Step Into Xcode"...

    "In a C program, this almost always means that a proper value has not been supplied for a pointer."

    I've gone through it and found that I get the error in the fscanf command...this is just the simple line I tried to compile...

    Any more help would be appreciated. I hate spending time on these computer using problems, I'd much rather spend my time learning to write code!

  5. Sep 17, 2008 #4


    User Avatar
    Science Advisor
    Homework Helper

    Check the return from fopen, input will be null if the open failed.
    if ( input = fopen("data.txt","r") ) { fscanf(input.....) }

    My guess would be that xcode puts the build in a debug subdir and data.txt is in the current dir.
  6. Sep 18, 2008 #5
    You sir, have made my week.

    You were correct. The program puts the code in a debug subdir where the data file was not. If I move the data file to the subdir, all is fine.

    Although that is somewhat inconvenient to do, it solves my problems for the near future.
  7. Sep 18, 2008 #6


    User Avatar
    Science Advisor
    Homework Helper

    There is probably an option in the debug setup somewhere to set the starting directory for the debug session.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook