turrier.fr

Source : ct|23.04.08

< Tutorials Computer, Multimedia, Chinese

Read and write binary files with C++

A binary file is a file containing a succession of bytes, where each byte can take a decimal value going from 0 to 255 (00 to FF in hexadecimal). In general, we assign an extension .bin or .dat to a binary file, when this does not require a specific extension (for use with a particular software). In this last case, we assign him an extension recognized by this software.

A binary file distinguishes himself from a text file (extension .txt) in the fact that it can contain bytes which are not recognized (as characters) by a text editor. For this reason a binary file must be readed either with a specific software who understands his format, or with a hexadecimal editor. In the last case, the hexadecimal editor displays the numerical value (00 to FF) of each contained bytes in the file.

The present tutorial shows, with the help of an example, a solution (it exists numerous others...) permitting to create binary files, then to read and to write bytes, inside those files, with the C++ language.

We will use the free development environment CodeBlocks, but an other development environment (Devcpp, Visual Studio Express...) can also be used.

Introduction

To write bytes in a binary file "fic1.dat", we will begin to write those bytes in a table int[100] in memory, then we will transfer the content of this table in the binary file.

To read bytes present in a binary file "fic1.dat", we will copy those values in a table in memory int t[100].

image1

The example hereafter::
1- Creates a file "fic1.dat", writes 10 bytes (0,1,2,3,4,5,6,7,8 and 9) in a table t[100], transfers the content of this table in the file "fic1.dat" then closes the file "fic1.dat" ;
2- Opens the file "fic1.dat", reads the 10 first contained bytes in this file, transfers them in a table T[100] then closes the file "fic1.dat";
3- Creates a file "fic2.dat", writes in this file the 10 bytes contained in the table T[100], after an addition of the value 10, then close the file "fic2.dat".

At last of the program, opening "fic.dat" and "fic2.dat" with the aid of a hexadecimal editor, we find:
- In the file "file1.dat", the 10 bytes 00 01 02 03 04 05 06 07 08 09
- In the file "file2.dat", the 10 preceding bytes increased of 10, so in other words 10 11 12 13 14 15 16 17 18 19 ( or 0A 0B 0C 0D 0E 0F 10 11 12 13 in hexadecimal).

The source code

#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>

int main()
{
FILE *fichier;
int i;
int t[100];
//-------------------------------------------------------
//create the file fic1.dat and write 10 octets
//------------------------------------------------------
cout<<"Ecriture de 10 octets dans fic1.dat\n";
for (i=0;i<10;i++){t[i]= i;}
fichier = fopen("fic1.dat","w+b");
for (i=0; i <10; i ++)
{
fprintf (fichier,"%c", t[i]);
}
fclose(fichier);
system("pause");
//----------------------------------------
//read bytes in the file fic1.dat
//--------------------------------------
cout<<"Lecture de 10 octets dans fic1.dat\n";
fichier=fopen("fic1.dat","r");
for (i=0; i <10; i ++)
{
fseek(fichier,i,SEEK_SET);
t[i]=fgetc(fichier);
}
fclose(fichier);
system("pause");
//-----------------------------------------------
//Create the file fic2.dat and write 10 bytes
//------------------------------------------------
for (i=0;i<10;i++){t[i]= 10+t[i];}
cout<<"Ecriture de 10 octets dans fic2.dat\n";
fichier = fopen("fic2.dat","w+b");
for (i=0; i <10; i ++)
{
fprintf (fichier,"%c", t[i]);
}
fclose(fichier);
cout<<"Fin du traitement\n";
system("pause");
return 1;
}

Run the program

After having compile and run the program, we obtain the following result.

image2

image3

image4

image5


Valid XHTML 1.0 Transitional

© http://turrier.fr (2007)