Problem with fopen in xcode (C)

  • Thread starter Thread starter chasely
  • Start date Start date
Click For Summary
The discussion revolves around troubleshooting a code issue in Xcode, specifically related to file handling. The user is unable to open a file named "data.txt" for reading, which leads to a breakpoint being hit at the `fopen` command. After addressing the breakpoint issue, the user encounters a "signal 10 (SIGBUS)" error during compilation, attributed to a pointer not being properly initialized. Suggestions from other participants include checking the return value of `fopen`, as it will be null if the file cannot be opened. It is identified that Xcode places the build in a debug subdirectory, while the data file resides in a different directory. The solution involves moving the data file into the debug subdirectory, which resolves the issue. The user notes that while this workaround is effective, it may be inconvenient, and hints at the possibility of adjusting the debug setup to change the starting directory for future sessions.
chasely
Messages
2
Reaction score
0
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.


/*
What is wrong with this in xcode?
*/


#include <stdlib.h>
#include <time.h>
#include <stdio.h>

int main() {

FILE *fp;
int tokens;
int wager;
int CompDie;
int UserDie;

fp = fopen ("data.txt","r");
srand((int)time(NULL) + 1);

fscanf (fp, "%d", &tokens);

printf ("%d", tokens);
fclose (fp);

printf ("Description\n");

printf ("What is your wager\n");
scanf ("%d", &wager);

UserDie = (rand() % 6) + 1; /* produce a random number 1 to 6 */
CompDie = (rand() % 6) + 1; /* what is the % used for? */

printf ("User: %d Computer: %d", UserDie, CompDie);

if (UserDie > CompDie)
tokens = tokens + wager;
if (CompDie > UserDie)
tokens = tokens - wager;

printf ("Tokens left: %d", tokens);

fp = fopen ("data.txt","w");
fprintf (fp, "%d", tokens);
fclose (fp);

return 0;

}

Picture2.png
 
Technology news on Phys.org
It stopped because you set a breakpoint?
Press f5 (or whatever in xcode) or clear the breakpoint.
 
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!

/*
What is wrong with this in xcode?
*/

#include <stdio.h>

int main() {

FILE *input;
int tokens;


input = fopen ("data.txt","r");

fscanf (input, "%d", &tokens);

return 0;

}
 
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.
 
mgb_phys said:
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.

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.
 
There is probably an option in the debug setup somewhere to set the starting directory for the debug session.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
5
Views
2K
Replies
14
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
7
Views
2K
  • · Replies 20 ·
Replies
20
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
2
Views
2K