Binary File Handling


FILE OPERATION ON BINARY FILES

#include<iostream.h>

#include<conio.h>

#include<fstream.h>

#include<iomanip.h>

const char *filename=”BINARY.TXT”;

int main()

{

float height[4]={175.5,153.0,167.25,160.70};

clrscr();

cout<<“\n\t\t FILE OPERATIONS ON BINARY FILES”;

cout<<“\n\t\t ~~~~ ~~~~~~~~~~ ~~ ~~~~~~ ~~~~~”;

ofstream outfile;

outfile.open(filename);

outfile.write((char *)&height,sizeof(height));

outfile.close();

for(int i=0;i<4;i++)

height[i]=0;

ifstream infile;

infile.open(filename);

infile.read((char *)&height,sizeof(height));

cout<<“\n\n The value of Height\n\n “;

for(i=0;i<4;i++)

{

cout.setf(ios::showpoint);

cout<<setw(10)<<setprecision(2)<<height[i];

}

infile.close();

getch();

return 0;

}

FILE OPERATIONS ON BINARY FILES

~~~~ ~~~~~~~~~~ ~~ ~~~~~~ ~~~~~

The value of Height

175.50    153.00    167.25    160.70

Advertisements