Page 1 of 1

Corrupt files

Posted: Wed Oct 12, 2005 6:57 am
by Xqzzy Rcxmcq
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.

Re:Corrupt files

Posted: Wed Oct 12, 2005 7:38 am
by AR
Did you open both files in binary mode?

Re:Corrupt files

Posted: Wed Oct 12, 2005 7:39 am
by df
sounds like you opened the file in text mode rather than binary mode...

Re:Corrupt files

Posted: Wed Oct 12, 2005 9:46 am
by Xqzzy Rcxmcq
Hey, looks like you're right. I feel kind of stupid now. How do you open them in binary mode? Thanks.

Re:Corrupt files

Posted: Wed Oct 12, 2005 10:20 pm
by Solar
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):

Code: Select all

FILE * in = fopen( "filename", "rb" );
http://www.dinkumware.com/manuals/reader.aspx?b=p/&h=fstream.html#basic_filebuf::open (C++):

Code: Select all

fstream in( "filename", ios_base::in | ios_base::binary );

Re:Corrupt files

Posted: Thu Oct 13, 2005 8:17 am
by mystran
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

Posted: Thu Oct 13, 2005 9:58 am
by Solar
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. ;)