std::string TilesetCopy = "Data//" + CurrentTileset + ".txt";
//make array to compensate for null terminator
char *File = new char[TilesetCopy.length() + 1];
for(int i = 6; i < TilesetCopy.length() + 1; i++)
File = TilesetCopy<i>;
printf(File);
</pre>
CurrentTileset is another string. It stores what tileset should be in memory. Currently it holds "World" when I prinf the File variable it's just some garbage looking stuff. I initially thought it may be the memory address but it was the same value each time I ran the program, hinting at my conversion was going to crap, not the outputting.
I've tried static casting TilesetCopy but that yielded the same thing.
Any help is appreciated.
edit:added space in File
<SPAN CLASS=editedby>[edited by - iron eye on November 28, 2003 2:13:06 AM]</SPAN> </i>
Converting strings to char arrays
I'm having problems converting a string which stores a filepath to an array of chars to be read by a file stream.
Here's what I've got it looks like it should work:
Maybe it should be:
File[i-6] = TilesetCopy <br><br><SPAN CLASS=editedby>[edited by - dcosborn on November 28, 2003 2:03:24 AM]</SPAN>
File[i-6] = TilesetCopy <br><br><SPAN CLASS=editedby>[edited by - dcosborn on November 28, 2003 2:03:24 AM]</SPAN>
The std::string type has a function to convert to c-style strings automatically. identifier.c_str(). If I understand your question correctly you should be able to use the provided function.
I cannot remember off the top of my head if the new c-style string will have the null character automatically appended to the end or not. You could check the MSDN for specifics. If not simply concat the null character with strcat().
If anyone else has another(and preferrably better) answer I would also be very interested to read it.
Acero
[edited by - AceroSBU on November 28, 2003 2:05:34 AM]
I cannot remember off the top of my head if the new c-style string will have the null character automatically appended to the end or not. You could check the MSDN for specifics. If not simply concat the null character with strcat().
If anyone else has another(and preferrably better) answer I would also be very interested to read it.
Acero
[edited by - AceroSBU on November 28, 2003 2:05:34 AM]
quote:Original post by dcosborn
Maybe it should be:
File[i-6] = TilesetCopy <br><br><SPAN CLASS=editedby>[edited by - dcosborn on November 28, 2003 2:03:24 AM]</SPAN><hr height=1 noshade></SPAN></BLOCKQUOTE><br><br>Gah…. I have no idea why I initialize i to 6….<br><br>I really have to stop coding when im tired.<br><br>I''ll look into that c_str() function, I''m sure it''s faster than my quick hack.
Don't mean to be picky, but your updated code is still wrong. You need to use the subscript operator to access each character in both File and TilesetCopy. So you need something like:
for (int i = 0; i < TilesetCopy.length() + 1; ++i) // If you only want to copy the stuff after "data//", use this File = TilesetCopy;<br><br> // If you want to copy the entire string as is, use this<br> File = TilesetCopy;<br> </pre> <br>Of course this doesn't matter if you go with AceroSBU's suggestion and use c_str(), which returns a c-style string with a null-terminator. I originally thought the reason you had i = 6 was that you only wanted the stuff after "data//" copied over. If that's what you want, you can use the string::substr(…) method. Or just add 6 to the pointer you get from c_str(). <br><br><SPAN CLASS=editedby>[edited by - dcosborn on November 28, 2003 2:41:26 AM]</SPAN>
I think c_str() and strcpy is the way to go.
Just use
To ignore the first five characters use:
Just use
strcpy(File, TileSetCopy.c_str())
. The terminating character should be added automagically.To ignore the first five characters use:
char *pTemp = TileSetCopy.c_str();char *File = new char[TileSetCopy.length() - 6 + 1];strcpy(File, &pTemp[6]);
Iron Eye, if you''d like more specific help through email I''d be willing to give it a try. Leave a message here if you want email help and I''ll unblock my address for 24 hours or so, so that you can take a look at it.
quote:Original post by ResearchMonkeyActually, that isn''t quite safe because you''re getting a reference to a char which could be anywhere, not neccesssarily in the internal array somewhere (afaik, maybe the standard says otherwise).
[...]To ignore the first five characters use:char *pTemp = TileSetCopy.c_str();char *File = new char[TileSetCopy.length() - 6 + 1];strcpy(File, &pTemp[6]);
The more proper copy line would be
strcpy(File, TileSetCopy.c_str()+5)
which ignores charracters #0-4 (the first five characters in the string). The only catch is that you have to make sure the string is long enough or else it could copy from arbitrary memory (same as in you example)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement