Anda di halaman 1dari 4

Binary File 01

This document is sharing some simple programs handling data in binary files.
Program 1:
This program is just reading & writing some variables in binary file
#include <iostream>
#include <fstream>

using namespace std;

int main(){
//openning binary file for writing
ofstream fileW("abc.bnr",ios::binary);
int i=478; float f=23.4567; char ch='F';
fileW.write((char*)&i, sizeof(i));
fileW.write((char*)&f, sizeof(f));
fileW.write(&ch, sizeof(ch));
fileW.close();
//openning binary file for reading
ifstream fileR("abc.bnr",ios::binary);
int i1; float f1; char ch1;
fileR.read((char*)&i1, sizeof(i1));
fileR.read((char*)&f1, sizeof(f1));
fileR.read(&ch1, sizeof(ch1));
fileR.close();
cout<<"Data Read From File\n";
cout<<"Integer:"<<i1<<'\n';
cout<<"Float:"<<f1<<'\n';
cout<<"Character:"<<ch1<<'\n';
return 0;
}
The output of this code is:

The size of this file is 9 bytes that is 4+4+1=9. Four bytes for integer, four bytes for float and one byte for
character. The notepad view of this file is:

Program 2:
This program is writing random size array in binary file without any count or size. Later, we are reading
all values in a dynamic array but before we are finding number of elements in file by using seek & tell
functions
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <fstream>

using namespace std;

void init(int x[], int S){


for (int i=0;i<S;i++)
x[i]=rand()%10000;
}
void print(int x[], int S){
for (int i=0;i<S;i++)
cout<<x[i]<<' ';
cout<<'\n';
}
int main(){
//openning binary file for writing
ofstream fileW("numbers.bin",ios::binary);
srand(time(0));
int count=rand()%40+10, i, *a;
cout<<"Count:"<<count<<'\n';
a=new int[count];
init(a, count);
print(a, count);
fileW.write((char*)a, sizeof(int)*count);
fileW.close();
//openning binary file for reading
ifstream fileR("numbers.bin",ios::binary);
int count1, *b;
1. fileR.seekg(0, ios::end);
count1=fileR.tellg()/sizeof(int);
cout<<"Count:"<<count1<<'\n';
b=new int[count1];
2. fileR.seekg(0);
fileR.read((char*)b, sizeof(int)*count1);
print(b, count1);
fileR.close();
return 0;
}
In this program, line number indicated by 1 is taking file pointer to end of file, whereas line 2 is getting
back pointer to zero, here second parameter is by default ios::beg. This operation is required because
read / write operation is performed from file pointer.
Program 3:
In this program we are writing multiple integers array, each of random size. Later, we are reading all
values in a dynamic array but before we are finding number of elements in file by using seek & tell
functions. Here, we are reading all data from file including both values written first & second.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <fstream>

using namespace std;

void init(int x[], int S){


for (int i=0;i<S;i++)
x[i]=rand()%10000;
}
void print(int x[], int S){
for (int i=0;i<S;i++)
cout<<x[i]<<' ';
cout<<'\n';
}
int main(){
//openning binary file for writing
ofstream fileW("numbers.bin",ios::binary);
srand(time(0));
int count=rand()%40+10, i, *a;
cout<<"Count:"<<count<<'\n';
a=new int[count];
init(a, count);
print(a, count);
fileW.write((char*)a, sizeof(int)*count);
delete []a;
count=rand()%40+10;
cout<<"Count Again:"<<count<<'\n';
a=new int[count];
init(a, count);
print(a, count);
fileW.write((char*)a, sizeof(int)*count);
fileW.close();
delete []a;
//openning binary file for reading
ifstream fileR("numbers.bin",ios::binary);
int count1, *b;
fileR.seekg(0, ios::end);
count1=fileR.tellg()/sizeof(int);
cout<<"Count:"<<count1<<'\n';
b=new int[count1];
fileR.seekg(0);
fileR.read((char*)b, sizeof(int)*count1);
print(b, count1);
fileR.close();
delete []b;
return 0;
}
Program 4:
In this program we are modifying file by changing negative data with positive data. In this case if
number is negative we are taking file pointer back and over writing the correct record at the place of
previous record, rest see the output.
void init(int x[], int S){ void print(int x[], int S){
for (int i=0;i<S;i++) for (int i=0;i<S;i++)
x[i]=rand()%100-50; cout<<x[i]<<' ';
} cout<<'\n';
}
void createFile(){
//openning binary file for writing
ofstream fileW("numbers.bin",ios::binary);
srand(time(0));
int count=rand()%40+10, i, *a;
cout<<"Count:"<<count<<'\n';
a=new int[count];
init(a, count);
print(a, count);
fileW.write((char*)a, sizeof(int)*count);
delete []a;
fileW.close();
}
void readAndPrintFile(){
ifstream fileR("numbers.bin",ios::binary);
int count1, *b;
fileR.seekg(0, ios::end);
count1=fileR.tellg()/sizeof(int);
cout<<"Count:"<<count1<<'\n';
b=new int[count1];
fileR.seekg(0);
fileR.read((char*)b, sizeof(int)*count1);
print(b, count1);
fileR.close();
delete []b;
}
void modifyFile(){
fstream file("numbers.bin",ios::binary|ios::in|ios::out);
int temp,filePosition;
while (true){
filePosition=file.tellg() ;
file.read((char*)&temp,sizeof(int));
if (file.eof())
break;//terminate loop
if (temp<0){
temp=-temp;//change negative into positive
file.seekp(filePosition);
file.write((char*)&temp,sizeof(int));
}
}
file.close();
}
int main(){
createFile();
readAndPrintFile();
modifyFile();
readAndPrintFile();
return 0;
}

Anda mungkin juga menyukai