next up previous contents
Next: Getting File Statistics Up: File Functions Previous: Reading and Writing Files

Binary Files

When you need to work with data files, you will need to know what binary mode is. There are two major differences between binary mode and text mode:

Note The examples in this section relate to the DOS operating system.

In order to demonstrate these differences, we'll use a data file called BINARY.DAT with the following contents:

01
02
03

First, we'll read the file in the default text mode.

We procede as follows:

The Perl to do this is, binary1.pl:

$buffer = "";



open(FILE, "<binary.dat");

read(FILE, $buffer, 20, 0);

close(FILE);



foreach (split(//, $buffer)) {

    printf("%02x ", ord($_));

    print "\n" if $_ eq "\n";

}

This program displays:

30 31 0a

30 32 0a

30 33 0a

This example does a couple of things that haven't been met before. The Read() function is used as an alternative to the line-by-line input done with the diamond operator. It will read a specified number of bytes from the input file and assign them to a buffer variable. The fourth parameter specifies an offset at which to start reading. In this example, we started at the beginning of the file.

The split() function in the foreach loop breaks a string into pieces and places those pieces into an array. The double slashes indicate that each character in the string should be an element of the new array.

Once the array of characters has been created, the foreach loop iterates over the array. The printf() statement converts the ordinal value of the character into hexadecimal before displaying it. The ordinal value of a character is the value of the ASCII representation of the character. For example, the ordinal value of '0' is 0x30 or 48.

The next line, the print statement, forces the output onto a new line if the current character is a newline character. This was done simply to make the output display look a little like the input file.

Now, let's read the file in binary mode and see how the output is changed.

The new code is as follow, binary2.pl:

$buffer = "";



open(FILE, "<binary.dat");

binmode(FILE); 

read(FILE, $buffer, 20, 0);

close(FILE);



foreach (split(//, $buffer)) {

    printf("%02x ", ord($_));

    print "\n" if $_ eq "\n";

}

This program displays:

30 31 0d 0a

30 32 0d 0a

30 33 0d 0a

When the file is read in binary mode, you can see that there are really two characters at the end of every line-the linefeed and newline characters.


next up previous contents
Next: Getting File Statistics Up: File Functions Previous: Reading and Writing Files
dave@cs.cf.ac.uk