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.
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.