Tuesday, 24 February 2015

Read() and Write() Functions Using C++

// Read and Write Functions

#include<iostream.h>
#include<fstream.h>
#include<conio.h>
main()
{
int var1,n1;
float var2,n2;
clrscr();

cout<<"\n Enter an Integer Value\n";
cin>>var1;
cout<<"\n Enter an Float Value\n";
cin>>var2;

ofstream out("MyFile.txt",ios::binary);

out.write((char*)&var1,sizeof(int));
out.write((char*)&var2,sizeof(float));
out.close();

ifstream in("MyFile.txt",ios::binary);
in.read((char*)&n1,sizeof(int));
in.read((char*)&n2,sizeof(float));
cout<<n1<<endl;
cout<<n2<<endl;
out.close();
}


No comments:

Post a Comment