Corrupt files

Programming, for all ages and all languages.
Post Reply
Xqzzy Rcxmcq

Corrupt files

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

Re:Corrupt files

Post by AR »

Did you open both files in binary mode?
User avatar
df
Member
Member
Posts: 1076
Joined: Fri Oct 22, 2004 11:00 pm
Contact:

Re:Corrupt files

Post by df »

sounds like you opened the file in text mode rather than binary mode...
-- Stu --
Xqzzy Rcxmcq

Re:Corrupt files

Post by Xqzzy Rcxmcq »

Hey, looks like you're right. I feel kind of stupid now. How do you open them in binary mode? Thanks.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:Corrupt files

Post 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 );
Every good solution is obvious once you've found it.
mystran

Re:Corrupt files

Post 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).
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:Corrupt files

Post 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. ;)
Every good solution is obvious once you've found it.
Post Reply