Problem with getc()

Member
Posts: 185
Joined: 2005.02
Post: #1
the problem being, its not working!
here is the relevant code:

Code:
FILE * fp;
fp = fopen("square.obj", "r");
if (fp == NULL)
{
    puts("Problem opening file");
       return;
}
char ch = getc(fp);

according to the debugger, the program gets stuck in getc(), specifically at "0x90036134 <+0052> lwz r2,4(r30)"

Why is this not working?
Quote this message in a reply
Moderator
Posts: 508
Joined: 2002.09
Post: #2
getc doesn't want a FILE as its argument. What you want is fscanf.

"When you dream, there are no rules..."
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #3
Taxxodium Wrote:getc doesn't want a FILE as its argument. What you want is fscanf.
man getc Wrote:int
getc(FILE *stream);

ferum: Your problem is on this line: if (fp = NULL)

Presumably you can spot it.
Quote this message in a reply
Member
Posts: 185
Joined: 2005.02
Post: #4
oops, thanks for catching that Thems, that didn't make the problem go away though... Sad
Quote this message in a reply
Moderator
Posts: 508
Joined: 2002.09
Post: #5
ThemsAllTook Wrote:ferum: Your problem is on this line: if (fp = NULL)

He has if (fp == NULL) which is correct!

I wouldn't use getc, instead try using fscanf like I said before.

PS: I didn't know getc accepted FILE's, my bad.

"When you dream, there are no rules..."
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #6
I would change
Code:
if (fp == NULL)
    puts("Problem opening file");

to

Code:
if (fp == NULL) {
    puts("Problem opening file");
    return/break/whatever;
}

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #7
Taxxodium Wrote:He has if (fp == NULL) which is correct!
It was originally if (fp = NULL). Apparently it was edited after I posted my message.

unknown's change is correct. Otherwise, your program will attempt to use fp even if it couldn't open the file, and will subsequently crash in getc(), as you're experiencing.
Quote this message in a reply
Member
Posts: 185
Joined: 2005.02
Post: #8
Ok I have edited the program (and my post to reflect my current code). I just assumed the program would print "problem opening file" regardless. It appears that fopen() is the culprit. After I inserted the return it started printing my "problem opening file message", meaning that fp is set to NULL.

Why isn't fopen working? Can it not find the file?
Where the %#$@ am I suppose to put the file? I have tried the debug folder, the user folder, the desktop. I have another program that writes creates a file to write to and later read. When I press the "build and go" button and run the program it creates the file in the Debug folder. But if I put the folder in the Debug folder it doesn't work. Or does my new program have a different default directory just because it is a GLUT program instead of a command line program?

Or can fopen() find the file but still decides to $<7M up anyways just for fun?

Hmm.. I don't know because neither the program nor the debugger are telling me anything is wrong!

ARG!!Mad
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #9
This has to do with how the working directory is set up when your program runs. Apparently, it's set to something different when you run your program from Xcode than when you run it from the Finder, and can be set to something else entirely if it's run from the Terminal.

If you don't mind using a little bit of Cocoa, doing this at the beginning of your program should do the trick:

Code:
#import <Cocoa/Cocoa.h>
#include <unistd.h>

// If you want to do stuff in your resources directory:
chdir([[[NSBundle mainBundle] resourcePath] fileSystemRepresentation]);

// If you want to do stuff in the directory containing your application bundle:
chdir([[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] fileSystemRepresentation]);
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Member
Posts: 185
Joined: 2005.02
Post: #11
:emoticon of a person smashing their head against a desk:
.app has "show package contents" on the menu!
gosh darnit!

Ok, putting the file in the reasources folder worked, even without the cocoa stuff.

Thanks for all the help!
Quote this message in a reply
Post Reply