• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

fopen() is being a dumb b!tch

Ultima

Platinum Member
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);
 
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)
 
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.
 
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..
 
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....
 


<< 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 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.
 


<<

<< 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
 


<< 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.
 
Back
Top