WHAT IS A BINARY FILE?
A binary file is a computer file that is not a text file. The term “binary file” is often used as a term meaning “non-text file
A binary file is a FILE whose content must be interpreted by a program or a hardware processor that understands in advance exactly how it is formatted. That is, the file is not in any externally identifiable format so that any program that wanted to could look for certain data at a certain place within the file. A program (or hardware processor) has to know exactly how the data inside the file is laid out to make use of the file.
In general, executable (ready-to-run) programs are often identified as binary files and given a file name extension of “.bin”. Programmers often talk about an executable program as a “binary” and will ask another programmer to “send me the binaries.” (A synonym for this usage is object code.) A binary file could also contain data ready to be used by a program.
In terms of transmitting files from one place to another, a file can be transmitted as a “binary,” meaning that the programs handling it don’t attempt to look within it or change it, but just pass it along as a “chunk of 0s and 1s,” the meaning of which is unknown to any network device.
WHAT IS BINARY FILE EDITOR
The Binary Editor allows you to edit any resource at the binary level in either hexadecimal or ASCII format. You can also use the Find command to search for either ASCII strings or hexadecimal bytes. Use the Binary Editor only when you need to view or make minor changes to custom resources or resource types not supported by the Visual Studio environment. The Binary Editor is not available in Express editions.
- To open the Binary Editor on a new file, go to menu File > New > File, select the type of file you want to edit, then select the drop arrow next to the Open button, and choose Open With > Binary Editor.
- To open the Binary Editor on an existing file, go to menu File > Open > File, select the file you want to edit, then select the drop arrow next to the Open button, and choose Open With > Binary Editor.
Binary data for a dialog box displayed in the Binary Editor
Only certain ASCII values are represented in the Binary Editor (0x20 through 0x7E). Extended characters are displayed as periods in the right panel ASCII value section of the Binary Editor. The printable characters are ASCII values 32 through 126.
Tip
While using the Binary Editor, in many instances you can right-click to display a shortcut menu of resource-specific commands. The commands available depend on what your cursor is pointing to. For example, if you right-click while pointing to the Binary Editor with selected hexadecimal values, the shortcut menu shows the Cut, Copy, and Paste commands.
WHAT IS BINARY FILE READER
Binary Viewer is a free windows utility allowing you to open and view any file located on your computer regardless of format file was saved. It can display data in decimal, octal, hexadecimal and text (ASCII or Unicode) formats.
What does a binary file contains?
bytesBinary files are usually thought of as being a sequence of bytes, which means the binary digits (bits) are grouped in eights. Binary files typically contain bytes that are intended to be interpreted as something other than text characters.
What is the binary file used for?
A binary file is one that does not contain text. It is used to store data in the form of bytes, which are typically interpreted as something other than textual characters. These files usually contain instructions in their headers to determine how to read the data stored in them.
Binary Files
Binary files are very similar to arrays of structures, except the structures are in a disk file rather than in an array in memory. Because the structures in a binary file are on disk, you can create very large collections of them (limited only by your available disk space). They are also permanent and always available. The only disadvantage is the slowness that comes from disk access time.
Binary files have two features that distinguish them from text files:
Binary files also usually have faster read and write times than text files, because a binary image of the record is stored directly from memory to disk (or vice versa). In a text file, everything has to be converted back and forth to text, and this takes time.
C supports the file-of-structures concept very cleanly. Once you open the file you can read a structure, write a structure, or seek to any structure in the file. This file concept supports the concept of a file pointer. When the file is opened, the pointer points to record 0 (the first record in the file). Any read operation reads the currently pointed-to structure and moves the pointer down one structure. Any write operation writes to the currently pointed-to structure and moves the pointer down one structure. Seek moves the pointer to the requested record.
Keep in mind that C thinks of everything in the disk file as blocks of bytes read from disk into memory or read from memory onto disk. C uses a file pointer, but it can point to any byte location in the file. You therefore have to keep track of things.
The following program illustrates these concepts:
#include <stdio.h> /* random record description - could be anything */ struct rec { int x,y,z; }; /* writes and then reads 10 arbitrary records from the file "junk". */ int main() { int i,j; FILE *f; struct rec r; /* create the file of 10 records */ f=fopen("junk","w"); if (!f) return 1; for (i=1;i<=10; i++) { r.x=i; fwrite(&r,sizeof(struct rec),1,f); } fclose(f); /* read the 10 records */ f=fopen("junk","r"); if (!f) return 1; for (i=1;i<=10; i++) { fread(&r,sizeof(struct rec),1,f); printf("%d\n",r.x); } fclose(f); printf("\n"); /* use fseek to read the 10 records in reverse order */ f=fopen("junk","r"); if (!f) return 1; for (i=9; i>=0; i--) { fseek(f,sizeof(struct rec)*i,SEEK_SET); fread(&r,sizeof(struct rec),1,f); printf("%d\n",r.x); } fclose(f); printf("\n"); /* use fseek to read every other record */ f=fopen("junk","r"); if (!f) return 1; fseek(f,0,SEEK_SET); for (i=0;i<5; i++) { fread(&r,sizeof(struct rec),1,f); printf("%d\n",r.x); fseek(f,sizeof(struct rec),SEEK_CUR); } fclose(f); printf("\n"); /* use fseek to read 4th record, change it, and write it back */ f=fopen("junk","r+"); if (!f) return 1; fseek(f,sizeof(struct rec)*3,SEEK_SET); fread(&r,sizeof(struct rec),1,f); r.x=100; fseek(f,sizeof(struct rec)*3,SEEK_SET); fwrite(&r,sizeof(struct rec),1,f); fclose(f); printf("\n"); /* read the 10 records to insure 4th record was changed */ f=fopen("junk","r"); if (!f) return 1; for (i=1;i<=10; i++) { fread(&r,sizeof(struct rec),1,f); printf("%d\n",r.x); } fclose(f); return 0; }
In this program, a structure description rec has been used, but you can use any structure description you want. You can see that fopen and fclose work exactly as they did for text files.
The new functions here are fread, fwrite and fseek. The fread function takes four parameters:
- A memory address
- The number of bytes to read per block
- The number of blocks to read
- The file variable
Thus, the line fread(&r,sizeof(struct rec),1,f); says to read 12 bytes (the size of rec) from the file f (from the current location of the file pointer) into memory address &r. One block of 12 bytes is requested. It would be just as easy to read 100 blocks from disk into an array in memory by changing 1 to 100.
The fwrite function works the same way, but moves the block of bytes from memory to the file. The fseek function moves the file pointer to a byte in the file. Generally, you move the pointer in sizeof(struct rec) increments to keep the pointer at record boundaries. You can use three options when seeking:
- SEEK_SET
- SEEK_CUR
- SEEK_END
SEEK_SET moves the pointer x bytes down from the beginning of the file (from byte 0 in the file). SEEK_CUR moves the pointer x bytes down from the current pointer position. SEEK_END moves the pointer from the end of the file (so you must use negative offsets with this option).
Several different options appear in the code above. In particular, note the section where the file is opened with r+ mode. This opens the file for reading and writing, which allows records to be changed. The code seeks to a record, reads it, and changes a field; it then seeks back because the read displaced the pointer, and writes the change back.
Binary file structure
One of the advantages of binary files is that they are more efficient.
In terms of memory, storing values using numeric formats such as IEEE 754, rather than as text characters, tends to use less memory.
In addition, binary formats also offer advantages in terms of speed of access. While the basic unit of information is very straightforward in a plain text file (one byte equals one character), finding the actual data values is often much harder. For example, in order to find the third data value on the tenth row of a CSV file, the reader software must keep reading bytes until nine end-of-line characters have been found and then two delimiter characters have been found. This means that, with text files, it is usually necessary to read the entire file in order to find any particular value.
For binary formats, some sort of format description, or map, is required to be able to find the location (and meaning) of any value in the file. However, the advantage of having such a map is that any value within the file can be found without having to read the entire file.
As a typical example, a standard feature of binary files is the inclusion of some sort of header information, both for the overall file, and for subsections within the file. This header information contains information such as the byte location within the file where a set of values begins (a pointer), the number of bytes used for each data value (the data size), plus the number of data values. It is then very simple to find, for example, the third data value within a set of values, which is: pointer + 2 x size.
More information is required in order to locate values within a binary format, but once that information is available, navigation within the file is faster and more flexible.
ADVANTAGES OF BINARY FILES
One of the advantages of binary files is that they are more efficient. In terms of memory, storing values using numeric formats such as IEEE 754, rather than as text characters, tends to use less memory. In addition, binary formats also offer advantages in terms of speed of access.
Drop your comment