Hello, I have reviewed the section on file input/output, and am currently trying to read a file one byte at a time, change that byte, and put it back in. I am having some problems, however.
I am simply adding a char (a) to each byte in the oData array.
The size variable reports a size that is different than the strlen(oData). Why is this?
I have seen example code (that will not compile with MSVC++ express 08) that uses file.get() and file.put() methods to get a byte and put a byte. That seemed to get the results I was looking for.
*I know I am supposed to check the file is actually open, but for the sake of brevity I've omitted it - I make sure the file is there (and not corrupt) before running the program. That is not an issue in this example.
I compiled and ran this code, and I now have consistent size prints for filesize and size of my oData array. I think the key line may have been:
oData = newchar[ size+1 ]; // for the '\0'
Didn't think of that. I also noticed, after reading the documentation on this site, that line 16 is not needed when you call open( ... ) with ios::ate - this positions the pointer at the end of file, and tellg() will return its position. Though this is probably good practice to use that call anyway, in case someone modifies the open() parameters.
However, during the hours I spent outside of this, I was able to get get() and put() to work - reading one byte at a time until fail() returned true (end of file I suppose).
Is there an advantage of using one over the other?