C String weirdness - Printable Version
+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Game Programming Fundamentals (/forum-7.html)
+--- Thread: C String weirdness (/thread-7709.html)
C String weirdness - Muffinking - Jun 10, 2002 02:34 PM
ok heres the deal, in my game editor I open and save the files the user works with automatically. And since I finally figured out how to create a reference to the files properly (with help, thanks guys) I wrote a little function that accepts to variables.
void getFSSpec(char path, char name)
now when I pass the path and the name of the file to this funtion it crops the path by taking off the app name and sticking the name of the file onto the end of it. this is all fine and dandy but when I pass this string:
it always has some weird crazy character at the end of it making it too long, so when I use strlen to get the length it returns one more than it should..
but when I pass this string:
it works perfectlys.... I have to subtract one from the length to make the loop work properly.. so I have to add an addition character to the WorldMap string..
I don't really understand it..
when I run through it with the debugger every opens up properly but when I just try and open the app it says it can't find the "Graphics file" which probably means the extra character added when I go through the debugger does not exist and it is slicing off part of the path name.
I am using Project builder 2.0 beta if your wondering.
Has this happened to anyone else? I am really confused.. the code seems fine and it works under the debugger, but it would be nice not to have to hack around the problem.
C String weirdness - GoodDoug - Jun 10, 2002 03:43 PM
I'm going to take a wild guess at the problem, but the issues you are seeing are related in any case.
Any cstring is defined to be an array of chars delimited by a '\0' char. Here's my guess as to what your problem is... you do a memcpy (or strncpy) of the path into your "path" variable, and then you forget to set the end of the string to be 0.
Here's an example of how I would do it:
The key here is the call:
aSemiPath[appNameIndex+1] = 0;
which null terminates the string...
I hope this helps
C String weirdness - OneSadCookie - Jun 10, 2002 04:09 PM
Could this be a C vs Pascal string thing? An HFS vs POSIX path thing? I'm not sure I understand what you're asking.