Archived

This topic is now archived and is closed to further replies.

Converting strings to char arrays

This topic is 5130 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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:
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[ i ] = TilesetCopy;

printf(File);
 
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[i] but that yielded the same thing. Any help is appreciated. edit:added space in File[ i ] [edited by - iron eye on November 28, 2003 2:13:06 AM]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
quote:
Original post by dcosborn
Maybe it should be:

File[i-6] = TilesetCopy[i ]

[edited by - dcosborn on November 28, 2003 2:03:24 AM]


Gah.... I have no idea why I initialize i to 6....

I really have to stop coding when im tired.

I''ll look into that c_str() function, I''m sure it''s faster than my quick hack.

Share this post


Link to post
Share on other sites
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[ i ] = TilesetCopy[ i + 6 ];

// If you want to copy the entire string as is, use this
File[ i ] = TilesetCopy[ i ];

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

[edited by - dcosborn on November 28, 2003 2:41:26 AM]

Share this post


Link to post
Share on other sites
I think c_str() and strcpy is the way to go.

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]);

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
quote:
Original post by ResearchMonkey
[...]To ignore the first five characters use:
char *pTemp = TileSetCopy.c_str();
char *File = new char[TileSetCopy.length() - 6 + 1];
strcpy(File, &pTemp[6]);

Actually, 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).

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)

Share this post


Link to post
Share on other sites