C String weirdness

Posts: 45
Joined: 2002.04
Post: #1
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.

any thoughts?


/* Drunk...... fix later.... */
Quote this message in a reply
Posts: 104
Joined: 2002.04
Post: #2
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:
// aPath is our full string path
// aSemiPath will get the path - name
// aName is the name of the file we are looking for

int end;
int appNameIndex;
for (end = strlen(aPath); end >= 0 && !found; end--)
    if (aPath[end] == '/')
          appNameIndex = end+1;
          found = true;

// now copy the path to our new (already
// allocated) variable
memcpy(aSemiPath, aPath, sizeof(char)*appNameIndex);
aSemiPath[appNameIndex+1] = 0;

getFSSpec(aSemiPath, aName);

The key here is the call:
aSemiPath[appNameIndex+1] = 0;
which null terminates the string...

I hope this helps
Quote this message in a reply
Posts: 5,143
Joined: 2002.04
Post: #3
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.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  formatting value in a string sefiroths 3 6,154 Dec 12, 2011 02:41 AM
Last Post: sefiroths
  Getting a value from a String Megamac04 5 6,113 Jan 31, 2010 04:23 PM
Last Post: smasher
  Parsing from a string to something faster? Madrayken 3 4,955 Aug 10, 2009 03:32 PM
Last Post: smasher
  Is -fconstant-string-class broken under the XCode/Next runtime Justin Brimm 17 12,664 Jun 12, 2008 01:04 PM
Last Post: Justin Brimm
  string to interger comparison? dareuhl 8 7,197 May 22, 2008 05:44 PM
Last Post: OneSadCookie