Corrupt files
Posted: Wed Oct 12, 2005 6:57 am
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.
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.