0% found this document useful (0 votes)
109 views9 pages

Mode Type Working Description

The document discusses binary file opening modes and functions for reading from and writing to binary files in C/C++. It describes the different modes for opening binary files, including wb for write binary, rb for read binary, ab for append binary, and wb+, rb+, ab+ for read/write modes. It also explains how to use fread() and fwrite() functions to read from and write to binary files, providing examples of writing structured data to a binary file and reading it back. Finally, it discusses using fseek() to move the file position pointer to access specific records in a binary file without reading the whole file sequentially.

Uploaded by

ANKUR CHOUDHARY
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views9 pages

Mode Type Working Description

The document discusses binary file opening modes and functions for reading from and writing to binary files in C/C++. It describes the different modes for opening binary files, including wb for write binary, rb for read binary, ab for append binary, and wb+, rb+, ab+ for read/write modes. It also explains how to use fread() and fwrite() functions to read from and write to binary files, providing examples of writing structured data to a binary file and reading it back. Finally, it discusses using fseek() to move the file position pointer to access specific records in a binary file without reading the whole file sequentially.

Uploaded by

ANKUR CHOUDHARY
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Binary File Opening Modes

Mode Type Working Description

If the file exists, its


wb contents are overwritten.
Open for writing in binary mode.
If the file does not exist, it
will be created.

rb Open for reading in binary mode. If the file does not exist,
fopen() returns NULL.
ab Open for append in binary mode. If the file does not exist, it
Data is added to the end of the file. will be created.

If the file exists, its


Open for both reading and writing in
wb+ contents are overwritten.
binary mode. And modifying existing
If the file does not exist, it
contents.
will be created.

rb+ Open for both reading and writing in If the file does not exist,
binary mode. fopen() returns NULL.

ab+ Open for both reading and appending If the file does not exist, it
in binary mode. will be created.
Reading and Writing a Binary File
Functions
fread()
and
fwrite()
are used for reading from and writing to a file on
the disk respectively in case of binary files.
Writing to a binary file
To write into a binary file, we need to use
the fwrite() function. The functions take four
arguments:
• address of data to be written in the disk
• size of data to be written in the disk
• number of such type of data
• pointer to the file where you want to write.
Syntax :
fwrite(addressData, sizeData, numbersData, pointerToFile);
Example Writing Binary File
#include <stdio.h>
#include <stdlib.h>
struct threeNum
{ int n1, n2, n3;
};
void main()
{
int n;
struct threeNum num;
FILE *fptr;
if ((fptr = fopen("program.bin","wb")) == NULL){
printf("Error! opening file");
exit(0); }
for(n = 1; n <= 5; n++) {
num.n1 = n;
num.n2 = 5*n;
num.n3 = 5*n + 1;
fwrite(&num, sizeof(struct threeNum), 1, fptr); } fclose(fptr);
}
Reading from a Binary file
Function fread() also take 4 arguments similar to
the fwrite() function as above.
fread(addressData, sizeData, numbersData,
pointerToFile);
Example Reading Binary File
#include <stdio.h>
#include <stdlib.h>
struct threeNum
{ int n1, n2, n3;
};
void main()
{
int n;
struct threeNum num;
FILE *fptr;
if ((fptr = fopen("program.bin",“rb")) == NULL){
printf("Error! opening file");
exit(0); }
for(n = 1; n <= 5; n++) {
fread(&num, sizeof(struct threeNum), 1, fptr);
printf("n1: %d\tn2: %d\tn3: %d", num.n1, num.n2, num.n3);
fclose(fptr);
}
Getting data from fseek()
• if we have many records inside a file and need
to access a record at a specific position, we
need to loop through all the records before it
to get the record.
• This will waste a lot of memory and operation
time. An easier way to get to the required
data can be achieved using fseek().
• As the name suggests, fseek() seeks the cursor
to the given record in the file.
Syntax of fseek()
fseek(FILE * stream, long int offset, int whence);

The first parameter stream is the pointer to the file.


The second parameter is the position of the record to be found, and
The third parameter specifies the location where the offset starts.

Different whence in fseek()


Whence Meaning
SEEK_SET Starts the offset from the beginning of the file.
SEEK_END Starts the offset from the end of the file.
SEEK_CUR Starts the offset from the current location of the
cursor in the file.
Example Program of fseek()
#include <stdio.h>
#include <stdlib.h>
struct threeNum
{ int n1, n2, n3; };
void main()
{ int n;
struct threeNum num;
FILE *fptr;
if ((fptr = fopen("program.bin","rb")) == NULL){
printf("Error! opening file");
exit(1); }
fseek(fptr, -sizeof(struct threeNum), SEEK_END);
for(n = 1; n < 5; ++n) {
fread(&num, sizeof(struct threeNum), 1, fptr);
printf("n1: %d\tn2: %d\tn3: %d\n", num.n1, num.n2, num.n3);
fseek(fptr, -2*sizeof(struct threeNum), SEEK_CUR); }
fclose(fptr);
}

You might also like