I've written a small "copy file" routine using C++. It opens one file and uses getc to get each character and then it opens the output file and uses putc to place this character in the output file. Basic I/O.
This works fine for text files, but on special files like JPEGs, Word DOC files, executables, WinZip archives, or any other kind of binary file, after the function is performed on the file, the copy is corrupt. Using Dos Edit, the files appear exactly the same, but their sizes are slightly off.
I'm guessing that it's getc/putc that's causing the corruption, but I might be wrong. How can I write the characters to the file and cause it to work correctly? Do I need to use low-level C++, Assembler, or what? I'm thinking that there are special file headers that getc and putc are leaving out and this is causing the problem.
I'm writing an encryption program, and whenever I write the code to decrypt the files, I'd like to be able to decrypt executables, DOCS, etc. without them being corrupt in the end. Could someone help me with this? Thanks.
Corrupt files
Re:Corrupt files
Hey, looks like you're right. I feel kind of stupid now. How do you open them in binary mode? Thanks.
Re:Corrupt files
It's in the standard lib documentation on fopen() / fstream.open().
http://www.dinkumware.com/manuals/reader.aspx?b=c/&h=stdio.html#fopen (C):
http://www.dinkumware.com/manuals/reader.aspx?b=p/&h=fstream.html#basic_filebuf::open (C++):
http://www.dinkumware.com/manuals/reader.aspx?b=c/&h=stdio.html#fopen (C):
Code: Select all
FILE * in = fopen( "filename", "rb" );
Code: Select all
fstream in( "filename", ios_base::in | ios_base::binary );
Every good solution is obvious once you've found it.
Re:Corrupt files
It might be worth noticing that this is generally not needed on platforms like Unix which use a single character as end of line. It is however good practice to use the binary switch on Unix as well, if there's any chance that the code be used on a platform that cares (say, Windows).
Re:Corrupt files
I think it's good practice simply because it's correct. Like free()ing memory at the end of the program, closing files and generally not assuming the OS will do things right if you don't tell it explicitly.
Every good solution is obvious once you've found it.