Reading from and writing to files mysteriously fails. Whats wrong?
On Win32 platforms, there’s a big difference between text files and binary files. For text files, the “\r\n” characters are translated into “\n” when read from disk, and the ^Z character is read as an end-of-file marker. For binary files, no such translation is used. Although this works great for text files, it really messes things up when you’re trying to read and write binary files. If the read or write does not abort prematurely because a ^Z was found in the file, you will almost definitely get incorrect bytes in the file due to \n -> \r\n translation. The problem is that Perl for Win32, and the C runtime library it uses, open file in text mode by default. For each file handle you use in Perl for binary data, you need to specify that the file handle is in binary mode. Fortunately, there’s a function, binmode, that does just that. See the perlfunc documentation file for details. This script copies one binary file to another.
On Win32 platforms, there’s a big difference between text files and binary files. For text files, the \r\n characters are translated into \n when read from disk, and the ^Z character is read as an end-of-file marker. For binary files, no such translation is used. Although this works great for text files, it really messes things up when you’re trying to read and write binary files. If the read or write does not abort prematurely because a ^Z was found in the file, you will almost definitely get incorrect bytes in the file due to \n -> \r\n translation. The problem is that ActivePerl, and the C runtime library it uses, open file in text mode by default. For each file handle you use in Perl for binary data, you need to specify that the file handle is in binary mode. Fortunately, there’s a function, binmode, that does just that. See the perlfunc documentation file for details. This script copies one binary file to another. Note its use of binmode to set the mode of the file handle.