#include <iostream>
#include <windows.h>
#include <fstream>
#include <string>
using namespace std;
class Entry
{
public:
char name[50];
int id;
float sal;
bool b;
Entry *next;
void clear(Entry *head,Entry *tail)
{
while(head!=NULL)
{
head=tail->next;
delete tail;
tail=head;
}
}
};
int main ()
{
ifstream myfile1;
ofstream myfile2;
char *buff="",temp[256]="";
int i,length;
Entry *head=NULL,*tail=NULL,*e=new Entry();
do
{
cout<<"Enter ur option"<<endl;
cout<<"1.Load data into file"<<endl;
cout<<"2.Save data into file"<<endl;
cout<<"3.Read data from file"<<endl;
cout<<"4.Close the file"<<"\n";
cin>>i;
switch(i)
{
case 1:
if(head==NULL)
{
head=tail=new Entry();
}
else
{
if(head->next==NULL)
head->next=tail->next=new Entry();
else
tail->next=new Entry();
tail=tail->next;
}
if(head==tail)
{
head->next=tail->next=NULL;
}
cout<<"Enter name\t";
cin>>tail->name;
cout<<"Enter Id\t";
cin>>tail->id;
cout<<"Enter salary\t";
cin>>tail->sal;
cin.clear();
cout<<"Enter Boolean value\t";
cin>>tail->b;
cin.clear();
break;
case 2:
myfile2.open ("C:\\example.bin",ios::app|ios::in|ios::ate);
tail=head;
while(head!=NULL)
{
myfile2<<head->name<<"\t";
myfile2<<head->id<<"\t";
myfile2<<head->sal<<"\t";
if((head->b==false)||(head->b==true))
{
myfile2<<head->b<<"\n";
cin.clear();
}
head=head->next;
}
myfile2.close();
head=tail;
e->clear(head,tail);
break;
case 3:
myfile1.open("C:\\example.bin",ios::out|ios::ate);
cout<<"the contents are:\n";
myfile1.seekg(0,ios::end);
length=myfile1.tellg();
if(length>0)
{
myfile1.seekg(0,ios::beg);
buff=new char[length];
memset(buff,0,length);
myfile1.read(buff,length);
cout<<buff<<"\n";
}
else
{
cout<<"file is empty\n";
}
delete []buff;
myfile1.close();
break;
case 4:
if(head==NULL)
e->clear(head,tail);
delete e;
exit(0);
default:
if(head==NULL)
e->clear(head,tail);
delete e;
exit(0);
}
}while(1);
return 0;
}
hi
Iam using fstreams to create a file with storing data of type string,integer,bool,float.once the file is created(C://..//example.txt) and enter the data and closed it.but when we open the file manually and modify it and save the file.But i don't want to change the file content manually.For that what can i do to my program.
the code is as follows....
can any one help me.
thanks in advance...