Problem with fopen in xcode (C)

  • Thread starter Thread starter chasely
  • Start date Start date
AI Thread 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.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top