Binary File I/O - C Language Basics - COFPROG

Binary File I/O - C Language Basics

Binary File  I/O Functions For C:

Binary files:


Binary files are very similar to arrays of structures, except the structures are in a disk-file rather than an array in memory. Binary files have two features that distinguish them from text files:



You can instantly use any structure in the file.
You can change the contents of a structure anywhere in the file.
After you have opened the binary file, you can read and write a structure or seek a specific position in the file. A file position indicator points to record 0 when the file is opened.

A read operation reads the structure where the file position indicator is pointing to. After reading the structure the pointer is moved to point at the next structure.

A write operation will write to the currently pointed-to structure. After the write operation the file position indicator is moved to point at the next structure.


Example of array in C programming


/* C PROGRAM FOR OPERATIONS ON  A BINARY FILE */
#include<stdio.h>
int main()
{
FILE *fp;
fp = fopen("dmc","wb");
if(!fp)
{
printf("\n ERROR OPENING FILE");
return -1;
}
printf("\n ERROR OPENING FILE");
scanf("%d %f", 
fprintf( fp , "%s" , "WELCOME FRIEND!");
fclose(fp);
return 0;

Binary File Functions

The C file system includes two important functions: fread() and fwrite(). These functions can read and write any type of data, using any kind of representation. Their prototypes are:

size_t fread(void *buffer, size_t size, size_t num,FILE *fp);

size_t fwrite(void *buffer, size_t size, size_t num, FILE *fp);

The fread() function reads from the file associated with fp, num number of objects, each object size bytes long, into buffer pointed to by buffer. It returns the number of objects actually read. If this value is 0, no objects have been read, and either end of file has been encountered or an error has occurred. You can use feof() or ferror() to find out which. Their prototypes are:

int feof(FILE *fp);

int ferror(FILE *fp);

The feof() function returns non-0 if the file associated with fp has reached the end of file, otherwise it returns 0. This function works for both binary files and text files. The ferror() function returns non-0 if the file associated with fp has experienced an error, otherwise it returns 0.

The fwrite() function is the opposite of fread(). It writes to file associated with fp, num number of objects, each object size bytes long, from the buffer pointed to by buffer. It returns the number of objects written. This value will be less than num only if an output error as occurred.

The void pointer is a pointer that can point to any type of data without the use of a TYPE cast (known as a generic pointer). The type size_t is a variable that is able to hold a value equal to the size of the largest object supported by the compiler. As a simple example, this program write an integer value to a file called MYFILE using its internal, binary representation.


Binary File I/O - C Language Basics
Binary File I/O - C Language Basics

Previous
Next Post »

BOOK OF THE DAY