Problem with fopen in xcode (C)

  • Thread starter Thread starter chasely
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around issues encountered when using the fopen function in Xcode to read from and write to a file named "data.txt". Participants explore problems related to file access, debugging, and error handling in C programming within the Xcode environment.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Exploratory

Main Points Raised

  • One participant reports difficulty opening a file in Xcode, noting that the code works in other environments but fails at the fopen command.
  • Another participant suggests that the issue may be related to a breakpoint set in Xcode, which can interrupt the execution flow.
  • A participant experiences a "signal 10 (SIGBUS)" error during compilation, indicating a potential issue with pointer values, specifically at the fscanf command.
  • One suggestion is to check the return value of fopen, as it will be null if the file opening fails, and to ensure that the file is located in the correct directory.
  • A participant confirms that moving the data file to the appropriate debug subdirectory resolves the issue, although they express concern about the inconvenience of this solution.
  • Another participant mentions the possibility of configuring the starting directory for the debug session in Xcode settings.

Areas of Agreement / Disagreement

Participants generally agree that the file path issue is a significant factor in the problems encountered, but there is no consensus on the best approach to handle file management in Xcode.

Contextual Notes

There are unresolved assumptions regarding the file path and the configuration of the Xcode environment that may affect file access. The discussion does not clarify the implications of the SIGBUS error beyond its association with pointer values.

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.
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
5
Views
2K
Replies
14
Views
4K
  • · 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