-
File IO / help please
hi,
I am making a little program that encodes text files, but I have a small problem.
The text file has this text: test line
My program output text: testlline
(there is no encodeing going on yet)
It replaces the spaces with the char infront of it.
here is my code:
while(!cfile.eof())
{
// Read Char //
cfile.seekg(file_pos);
cfile >> char_buffer;
// Encode Char //
int_buffer = int(char_buffer);
//int_buffer+=-1;
// Write Char
cfile.seekp(file_pos);
char_out = char(int_buffer);
cfile << char_out;
file_pos++;
}
I am trying to leave the spaces as spaces, or even better, encode them too. Is my program skipping the spaces because of the code: cfile >> char_buffer; ?? if so, what way can I input text from a file?
thanks alot!
-
you might want to use the function member function 'get(char *, int)', (or since you seem to be inputting a single character at a time, 'get(char &ch)') to read in data.
Code:
while(!cfile.eof())
{
// Read Char //
cfile.seekg(file_pos);
cfile.get(char_buffer);
// Encode Char //
int_buffer = int(char_buffer); //casting isnt necessary here, char_buffer += -1, or simply char_buffer-- will suffice
//int_buffer+=-1;
// Write Char
cfile.seekp(file_pos);
char_out = char(int_buffer);
cfile << char_buffer;
file_pos++;
}
might I suggest you read in a few more char's than just one at a time, like say 1000 (or more, depending on file size), and encoding the whole buffer, it will go much quicker