fopen() is being a dumb b!tch

Ultima

Platinum Member
Oct 16, 1999
2,893
0
0
WTF, why doesn't this work??
The file opens fine the first time, but absolutely refuses to open the second time!! The calls to fopen are the EXACT SAME, so I fail to see why this won't work. Btw, you can ignore the stzLib thing, all that does is return the current directory (which is set only once, and is not calculated for every call).

FILE *whim = fopen(strcat(stzLib->GetRootDir(),"\\.\\..\\data\\media.stz"), "rb");
if (whim == NULL)
MessageBox(NULL,"WHIM!!!!",NULL,MB_OK);
fclose(whim);
whim = NULL;

whim = fopen(strcat(stzLib->GetRootDir(),"\\.\\..\\data\\media.stz"), "rb");
if (whim == NULL)
MessageBox(NULL,"MLAH!!!!",NULL,MB_OK);
 
Apr 5, 2000
13,256
1
0
I don't know what programming language that is, nor am I an expert in programming languages, but I think it might have something to do with the whim = NULL - bottom line, first paragraph. Since whim is going to equal to whatever "strcat(stzLib->GetRootDir(),"\\.\\..\\data\\media.stz"), "rb");" is, do you really need it? (I'm guessing you may if whatever that 2nd fopen gets is empty)
 

Sir Fredrick

Guest
Oct 14, 1999
4,375
0
0
I don't use fopen, I tend to use fstreams for my purposes, but no matter what you're using to access files, you need to close the file when you're done with it. fopen is probably failing the second time because that file is already open.
 

Ultima

Platinum Member
Oct 16, 1999
2,893
0
0
Nope, that's not why.
I figured it out, the solution was extremely gay though.

For whatever reason, fopen didn't like the "\.\..\" I was prefacing my files with. I have NO idea how else to append the filename when it's a directory below the one I'm in, so I've temporarily killed the directory stuff and just put "..\data\media.stz", which works for some reason.

This is retarded though and I wish I knew why fopen didn't like me using the \.\..\ there..
 

Ultima

Platinum Member
Oct 16, 1999
2,893
0
0
Sorry, this is more a result of my own stupidity actually.

I was under the impression that strcat() returned a new string when what it really does is modify the first one.
Ugh....
 

Sir Fredrick

Guest
Oct 14, 1999
4,375
0
0


<< Sorry, this is more a result of my own stupidity actually.

I was under the impression that strcat() returned a new string when what it really does is modify the first one.
Ugh....
>>



that doesn't really explain why it worked the first time.

edit:

ok, I see how strcat works now. Odd behavior, that it modifies the string and returns exactly the same thing.
your GetDir() function must return a pointer to a string, and so strcat is actually able to modify it. interesting...
 

Ameesh

Lifer
Apr 3, 2001
23,686
0
0
i know alot of times if i cant open a file the second time, its because i didnt close it properly the first time so the OS still has write file pointer to it and wont let you get access again bfor obvious reasons, i know you solved yours but in case you run into it again.
 

Ultima

Platinum Member
Oct 16, 1999
2,893
0
0


<<

<< Sorry, this is more a result of my own stupidity actually.

I was under the impression that strcat() returned a new string when what it really does is modify the first one.
Ugh....
>>



that doesn't really explain why it worked the first time.

edit:

ok, I see how strcat works now. Odd behavior, that it modifies the string and returns exactly the same thing.
your GetDir() function must return a pointer to a string, and so strcat is actually able to modify it. interesting...
>>



I wonder what was going on in their heads when those guys at ANSI or Microsoft were writing these libraries... :Q
 

Ultima

Platinum Member
Oct 16, 1999
2,893
0
0


<< i know alot of times if i cant open a file the second time, its because i didnt close it properly the first time so the OS still has write file pointer to it and wont let you get access again bfor obvious reasons, i know you solved yours but in case you run into it again. >>



Yeah, those pointers can be tricky ;)

Anyway, thanks everyone for the replies.