[note 1]
fstream
- We must include this file in any program that uses files.
[note 2]
Here are 2 ways to open a file.
- opening files using constructor
(useful when manipulate 1 file)
[1]
ofstream outfile("result"); //output only
- outfile is an object of ofstream
[the program may contain statements like]:
outfile<<"total";
outfile<<sum;
infile>>number;
[2]
ifstream iffile("data"); //both output and input
- opening file with open()
[example]
ofstream outfile;
outfile.open("data1");
...
outfile.close();
outfile.open("data2");
...
outfile.close();
//Reads the file created in Program 11.2
#include<iostream>
#include<fstream>
#include<stdlib.h> //for exit() function
using namespace std;
int main()
{
const int SIZE=80;
char line[SIZE];
ifstream fin1,fin2; //create two input streams
fin1.open("country");
fin2.open("capital");
for(int i=1;i<=10;i++)
{
if(fin1.eof()!=0)
{
cout << "Exit from country \n";
exit(1);
}
fin1.getline(line,SIZE);
cout << "Capital of " << line;
if(fin2.eof()!=0)
{
cout << "Exit from capital\n";
exit(1);
}
fin2.getline(line,SIZE);
cout << line << "\n";
}
return 0;
}
[note 3]
fin.eof() //return non-zero value when reach the end of a file
fin //return zero when reach the end of a file
[note 4]
file mode parameters
app ate binary in out trunc nocreate noreplace
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream ioFile; //L1
ioFile.open("C:\\dk\\a.dat",ios::out); //L2
ioFile<<"Alexis"<<" "<<76<<" "<<98<<" "<<67<<endl; //L3
ioFile<<"Bella"<<" "<<89<<" "<<70<<" "<<60<<endl;
ioFile<<"Charlie"<<" "<<91<<" "<<88<<" "<<77<<endl;
ioFile<<"David"<<" "<<62<<" "<<81<<" "<<75<<endl;
ioFile<<"Emily"<<" "<<90<<" "<<78<<" "<<67<<endl;
ioFile.close(); //L4
ioFile.open("C:\\dk\\a.dat",ios::in|ios::binary); //L5
char name[10];
int chinese,math,computer;
cout<<"Name\t"<<"English\t"<<"Mathematics\t"<<"Computer\t"<<"Total"<<endl; //Display caption
ioFile>>name; //L6
while(!ioFile.eof()) //L7
{
ioFile>>chinese>>math>>computer; //L8
cout<<name<<"\t"<<chinese<<"\t"<<math<<"\t"<<computer<<"\t"
<<chinese+math+computer<<endl;
ioFile>>name;
}
ioFile.close(); //L9
return 0;
}
[note 5]
Functions for manipulation of file pointers
seekg()/seekp()/tellg()/tellp()
infile.seekg(10);
// point to the 11th byte.
seekg(10,ios::beg);
[example]
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class Employee
{
private:
int number ,age;
char name[20];
double sal;
public:
Employee(){}
Employee(int num,char* Name,int Age, double Salary)
{
number=num;
strcpy(name,Name);
age=Age;
sal=Salary;
}
void display(){cout<<number<<"\t"<<name<<"\t"<<age<<"\t"<<sal<<endl;}
};
int main()
{
ofstream out("Employee.dat",ios::out|ios::binary);
Employee e1(1,"Frank",23,2320);
Employee e2(2,"Grace",32,3210);
Employee e3(3,"Harry",34,2220);
Employee e4(4,"Jordan",27,1220);
out.write((char*)&e1,sizeof(e1));
out.write((char*)&e2,sizeof(e2));
out.write((char*)&e3,sizeof(e3));
out.write((char*)&e4,sizeof(e4));
Employee e5(3,"Harry",40,2220);
out.seekp(2*sizeof(e1));
out.write((char*)&e5,sizeof(e5));
out.close();
ifstream in("Employee.dat",ios::in|ios::binary);
Employee s1;
cout<<"\n-------The data of the third person read from the file-----\n\n";
in.seekg(2*(sizeof(s1)),ios::beg);
in.read((char*)&s1,sizeof(s1));
s1.display();
cout<<"\n---------All the data read from the file------\n\n";
in.seekg(0,ios::beg);
in.read((char*)&s1,sizeof(s1));
while(!in.eof())
{
s1.display();
in.read((char*)&s1,sizeof(s1));
}
}
[note 6]
I/O operations
[1]
cin/cout
[2]
put()/get()
#include<iostream.h>
#include<fstream.h>
#include<string.h>
int main()
{
char string[80];
cout << "Enter a string \n";
cin >> string;
int len=strlen(string);
fstream file; //input and output stream
file.open("Text", ios::in|ios::out);
//On receieving the string, the program writes it, character by character,to the file using the put() function in a for loop
//The length of the string is used to terminate the for loop.
for(int i=0;i<len;i++)
file.put(string[i]); //put a character to file
file.seekg(0); //go to the start
char ch;
while(file)
{
file.get(ch); //get a character from file
cout << ch; //display it on screen
}
return 0;
}
[3]
write()/read()
[form]:
infile.read((char*)&V,sizeof(V));
outfile.write((char*)&V,sizeof(V));