OOP Lab: File operation

/* Write to a file */

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ofstream of(“test”,ios::app);
char name[30];
cout<<“Enter name:”<<endl;
cin>>name;
of<<name<<“\n”;
of.close();

return 0;
}

 

/* Read file */

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream ifs(“test”);
char name[30];
cout<<“File data:” <<endl;

ifs>>name;
while(ifs.eof() == 0){
cout << name <<” “;
ifs>>name;
}

ifs.close();

return 0;
}

 

/* Read as line */

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream ifs(“test”);
int SIZE = 80;
char name[SIZE];
cout<<“File data:” <<endl;

do{
ifs.getline(name,SIZE);
cout<< name <<endl;
}while(ifs.eof() == 0);

ifs.close();

return 0;
}

 

/* cursor movement */

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ofstream of(“test”,ios::app);
char name[30];
cout<<“Enter name:”<<endl;
cin>>name;
of.seekp(-5,ios::cur);
of<<name<<“\n”;
of.close();

return 0;
}

Posted in CSE

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>