Ofstream char problems

Hey.
I have some problems writing char to a file with ofstream.
this is how the code looks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
char buffer[5001];
char secondbuffer[5001];
char temp;
ifstream in(Filename here);
int i = 0;
while(in.get(secondbuffer) && !in.eof[])
{
i++;
}
for(int j = 0; j < i; j++)
{
secondbuffer[j] = buffer[j];
}
ofstream fout(somefile);
fout << secondbuffer;

// end of program 



The problem is that it reads the characters of the first file fine, but when it writes to the second file, it adds all characters from the first file, but when there are no more characters, it adds a lot of "Ì" characters in the end of file.

fx:
file 1:
abc
file 2:
abcÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ...

How can I prevent it?
Mark the end of the string with a '\0'.
Have tried that, and it doesn't work. But I know what you mean.
The code in my question is only supposed to illustrate what is going on, but the real code, the program "encryptes" the characters.
If you're using C++ streams why not use std::string, too?

1
2
3
4
5
6
ifstream  in( "filename.in" );
ifstream out( "filename.out" );
string line;
while( getline( in, line ) ) {
    out << line;
}


EDIT: I should mention that std::string does not need to be null-terminated, so it will retain length information for you--that's why this would work, similar to marking the end of the string.
Last edited on
#moorecm
Uhm,
I'm using one character of the text at a time, and decrypt it with a function. Thats why I dont use strings.
Topic archived. No new replies allowed.