I have a text file and read from it character by character. But I would like to concatenate these characters and have an array of characters.
As far as I can understand, I should use strcat. But I fail to to convert a char read from a file into a const char* so that I could use strcat:
char * strcat ( char * destination, const char * source );
In the debugger I can see that chr has "Bad Ptr". Will you be so kind as to help me?
ifstream infile; infile.open( "Gmv.txt", ifstream::in); char result[1000]; while (infile.good()) { character = infile.get(); const char * chr = reinterpret_cast<const char *>(character); strcat(result, chr); } infile.close();
while (infile.good())orwhile (!infile.eof()). It will loop once to many unless you have an extra check inside the loop. The reason is that when you read and hit the end of the file, the end-of-file flag is not set. Instead it is set the next time you attempt to read. Unfortunately it's a little harder when reading character-by-character, as you have to add a check after you read the character and break out of the loop if there is an error or end of file condition.