Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

Reading from and writing to files mysteriously fails. Whats wrong?

0
Posted

Reading from and writing to files mysteriously fails. Whats wrong?

0

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.

0

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.

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123