Converting a char* to a string in C++

Mister Sheep

Junior Member
Nov 30, 2007
8
0
0
So I have looked everywhere and cannot find a way to do this. I am using VC++ Express 2005 to develop my code.

I am using the function strtok_s() to break apart a char*, and the return type of strtok_s is a char*.

I have the following code:

>>>string final ;
>>>char tmp1[100] ;
>>>char* tmp2 ;
>>>char* next ;
>>>
>>>tmp2 = strtok_s(tmp1, " ", &next) ;
>>>cout << "tmp2 = " << tmp2 << endl ;

I then want to assign whatever is in tmp2 (the output from the cout function from what I see is what I want) to final, doing this:

final = tmp2 ;

However, when I pass final to one of my functions that takes a string the function breaks. An error box pops up saying:

>>>Debug Assertion Failed!
>>>
>>>Program: ...
>>>File: memcpy_s.c
>>>Line: 55

>>>Espression: dst != NULL

>>>For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts.

What is going on? And how can I make it so that final (when passed to my function that is basically defined as foo(string x) {} ) will work properly?

Any help is greatly appreciated, thanks a ton!
 

yinan

Golden Member
Jan 12, 2007
1,801
2
71
Been awhile since I have done any C++ but my first idea is instead of copying the pointer can you use strcopy to copy the returned string into a char array?
 

Mister Sheep

Junior Member
Nov 30, 2007
8
0
0
Close... except for the exact opposite, lol. strcpy() can take a string in as a source but can only output a char*. I actually do this earlier on in my code to convert my data input from a string to a char* for manipulation using strtok_s(). However, I then need to convert it back to a string once I do all the data manipulation I want on it.
 

JACKDRUID

Senior member
Nov 28, 2007
729
0
0
what are the contents in final? did you check?
its been a whiile but perhaps final=&tmp2; would help assign final with reference of tmp2 since tmp2 is a pointer..

can you post the complete code so I can try running/debugging it?
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
char *strCharacter = "Test String"; // null terminated
string strTest(strCharacter); // until null termination
string strTest2(strCharacter,5); // only 5 characters are used
 

Mister Sheep

Junior Member
Nov 30, 2007
8
0
0
void getHighScore_File(char* fileName) {
//Gets the high score file and pulls it into the program
int i = 0 ;
fstream file;
string something ;
string array[theCount] ;
char seps[] = " " ;
string debug1 ;
char tmp1[100] ; //Hope no one wants a name longer than 100 chars... cause this will break
char* tmp2 ;
char* next ;

file.open(fileName, fstream::eek:ut | fstream::in | fstream::app) ;
if (!file) {
cout << "Uh oh... you bwoke it." << endl ;
return ;
}
while(!file.eof()) { //What's wrong here it's not getting the information from the file
if (i >= theCount)
break ;
getline(file, array) ;
cout << array << endl ;
i++ ;
}
file.close() ;

for(int j = 0; j < theCount ; j++) {
strcpy_s(tmp1, array[j].c_str()) ;
tmp2 = strtok_s(tmp1, seps, &next) ;
cout << "tmp2 = " << tmp2 << endl ;

debug1 = tmp2 ;
cout << "debug1 = " << debug1 << endl ;
//highScore.setName(debug1) ;
highScore.setScore(atoi(strtok_s(NULL, seps, &next))) ;
cout //<< "name = " << highScore.getName() << endl
<< "score = " << highScore.getScore() << endl ;
addNewScore("ffs", highScore.getScore()) ;
}

}

So there is the code as I have it right now. highScore.setName() takes a string as it's parameter.

Any new tips or ideas?
 

Kyteland

Diamond Member
Dec 30, 2002
5,747
1
81
Originally posted by: Mister Sheep
I then want to assign whatever is in tmp2 (the output from the cout function from what I see is what I want) to final, doing this:

final = tmp2 ;
final.assign(tmp2);

I highly recommend this book if you are doing anything with the standard library.

Edit: Hey, I just tried your code and it works for me. final = tmp2; should be equivalent to final.assign(tmp2); The string class had an operator= function defined for char*;
 

Venix

Golden Member
Aug 22, 2002
1,084
3
81
Some suggestions:

fileName should be const.

Use a vector instead of a fixed-size array for the lines.

Use a vector for tmp1. Using a fixed-size buffer and overrunning/crashing if the line is longer than 100 characters is terrible style and a huge security hole.

file.open(..., out | in | app) will never fail, because it will create a file for writing if it doesn't exist. Use ifstream or fstream.open(..., fstream::in) to open a file for reading and fail if the file doesn't exist.

You are iterating over "theCount" strings in the array, but I see nothing guaranteeing that there will be that many read from the file. strok_s will return a null pointer if you give it an empty string, and assigning a null pointer to an std::string will cause a crash, so perhaps that is related to your problem.

I would also suggest using boost.tokenizer instead of strtok, if possible.
 

JACKDRUID

Senior member
Nov 28, 2007
729
0
0
string array[theCount] ; // theCount is not initialized...

if you are only readiing from a file, use ifstream


also.. comment out this part:

while(!file.eof()) { //What's wrong here it's not getting the information from the file
If (i >= theCount) Then
break ;
getline(file, array) ;
cout << array << endl ;
i++ ;
}

replace with

string x;
file >> x;
cout << x << endl;

what does it return? it should return first line of file... if not, then there is something wrong with your file declaratiion.


 

Mister Sheep

Junior Member
Nov 30, 2007
8
0
0
I did that when I was constructing that loop, I made sure that what was being pulled into array was correct. Also the fact that when I run the code through in debug mode I can see what variables are being set and where and they all looked good. Also the theCount is defined it is #defined theCount 10 at the top of my file.
 

Mister Sheep

Junior Member
Nov 30, 2007
8
0
0
void setName(string name) {
cout << "setting the name to - " << name << endl ;
_name = name ;
cout << "the name wsa successfully set." << endl ;
return ;
}

There is my code for setName... when I run my code up to this point it gets what appears to be the correct name into the function, however, it crashes on the line with "_name = name ;"

Mind boggling.
 

Venix

Golden Member
Aug 22, 2002
1,084
3
81
Yes, the problem is with _name. You aren't using malloc or memset or something, are you?

Can you just post all the code?
 

Mister Sheep

Junior Member
Nov 30, 2007
8
0
0
Here is the code... I think I figured out something that is wrong.

I'm never actually getting all the data from the file, it runs the loops but never fills up the array... and I don't know why.

Thanks again for all the help!

Edit: Wow... posting that code like that is really horrible
 

Mister Sheep

Junior Member
Nov 30, 2007
8
0
0
Ok... so I decided to completely start over, and really think the function out entirely this time.

And guess what... it works. I have discovered tonight (at least for myself) that a beer or two later I become a smarter programmer. LOL

Thanks again to everyone who responded to this forum I really appreciate it.