0

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(); 
2
  • Reading from a file character by character is not efficient, btw. You should read into a buffer in large chunks. Commented Apr 7, 2013 at 10:53
  • A tip: Don't loop while (infile.good()) or while (!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. Commented Oct 4, 2013 at 5:27

2 Answers 2

2

Assuming your files is 999 chars or less, this should work (no error checks added). There is no need to use strcat. As a matter of fact, it's stupid to use strcat here.

ifstream infile; infile.open( "Gmv.txt", ifstream::in); char result[1000]; int i = 0; while (infile.good()) { result[i] = infile.get(); ++i; } result[i] = 0; // Add the '\0' at the end of the char array read. infile.close(); 

strcat takes a char array terminated by 0 ('\0') as the 2nd param. your char isn't terminated by 0. Hence you get the bad pointer error.

BTW, you can shorter the while to this

while (infile) result[i++] = infile.get(); 
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you this is what I was looking for. Could you clarify what that result[i] = 0 is necessary for? I tried with and without it. Without it I get a garbage beyond meaningful characters when I run cout << result. But the mechanics of this is a mystery to me.
Most C and C++ functions expect a '\0' (same as 0) at the end of char arrays so that they know where the array ends. cout also uses the '\0' to find the end of the array. If you don't add the 0, cout will keep printing till it finds a naturally occuring 0.
1

Why use an array as a string, when C++ have std::string:

std::string result; char ch; while (infile >> ch) result += ch; 

2 Comments

Teachers for unknown reasons forbid to use string to beginners.
@Trts for very good reasons. the underlying string implementation is an array. such string classes abstract all of that away, so you never gain an appreciation or understanding of what's happening under the hood to make your code work. Furthermore, there are times when you cant use strings and must rely on the basics.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.