Converting strings to char arrays

Started by
6 comments, last by Iron Eye 20 years, 4 months ago
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 = 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 &#111;n November 28, 2003 2:13:06 AM]</SPAN>  </i>  
---
ConPong _//_ Google _//_ Chaos Forge - quick and easy file hosting for developers

"Games usually keep me from making my own..."
-Me
---



find your elementat mutedfaith.com.
Advertisement
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>
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]
quote:Original post by dcosborn
Maybe it should be:

File[i-6] = TilesetCopy <br><br><SPAN CLASS=editedby>[edited by - dcosborn &#111;n 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.
---
ConPong _//_ Google _//_ Chaos Forge - quick and easy file hosting for developers

"Games usually keep me from making my own..."
-Me
---



find your elementat mutedfaith.com.
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 &#111;nly 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
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 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)
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk

This topic is closed to new replies.

Advertisement