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();
Trts
  • 985
  • 1
  • 11
  • 24
  • Reading from a file character by character is not efficient, btw. You should read into a buffer in large chunks. – Patashu Apr 07 '13 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. – Some programmer dude Oct 04 '13 at 05:27

2 Answers2

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();
user93353
  • 13,733
  • 8
  • 60
  • 122
  • 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. – Trts Apr 07 '13 at 10:58
  • 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. – user93353 Apr 07 '13 at 10:59
  • @Trts - Check this http://stackoverflow.com/questions/10943033/why-are-strings-in-c-usually-terminated-with-0 – user93353 Apr 07 '13 at 11:02
1

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

std::string result;
char ch;

while (infile >> ch)
    result += ch;
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Teachers for unknown reasons forbid to use string to beginners. – Trts Apr 07 '13 at 10:56
  • @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. – ash Oct 03 '13 at 22:13