Transcript Click here

File Processing in C++
Data is stored in files so that it may be
retrieved for processing when needed
1
File Processing in C++
C++ provides the following classes to
perform output and input of characters
to/from files:
• ofstream: Stream class to write on files
• ifstream: Stream class to read from files
• fstream: Stream class to both read and
write from/to files.
2
Open a file
• Each one of the open() member functions of the
classes ofstream, ifstream and fstream has a
default mode that is used if the file is opened
without a second argument:
class
ofstream
ifstream
fstream
3
default mode parameter
ios::out
ios::in
ios::in | ios::out
READING DATA FROM
FILES USING C++
• The commands for reading data from a file
are (for the most part) very similar to those
used to read data from the keyboard.
4
READING DATA FROM FILES USING C++
• In order for your program to read from the file, you
must:





include the fstream header file
declare a variable of type ifstream
open the file
read from the file
after each read, check for end-of-file using the eof() member
function
 close the file when access is no longer needed (optional, but a
good practice)
5
Example
• Q1: Write a program that reads integer numbers
from the following file and print the result in
output screen.
6
READING DATA FROM FILES USING C++
#include<iostream>
#include <fstream>
using namespace std;
int main()
{ ifstream indata;
// indata is like cin
int num;
// variable for input value
indata.open(“example.txt",ios::in);
indata >> num;
while ( !indata.eof() )
{
cout << "The next number is " << num << endl;
indata >> num;
}
indata.close();
cout << "End-of-file reached.." << endl;
}
7
READING DATA FROM FILES USING C++
The result:
Example
• Q2: Write a program that reads characters from
the following file as long as the character not Q.
and print the result in output screen.
9
READING DATA FROM FILES USING
C++
#include<iostream>
#include <fstream>
using namespace std;
void main()
{ ifstream indata;
char letter;
indata.open("example.txt“,ios::in);
indata >> letter;
while ( letter != 'Q' )
{
cout << letter;
indata >> letter;
}
cout << endl;
indata.close();
cout << "End-of-file reached.." << endl;
10
}
// indata is like cin
// variable for input value
// opens the file
READING DATA FROM FILES USING C++
The result:
WRITING DATA IN THE FILES USING C++
• In order for your program to writein the file, you must:
 include the fstream header file
 declare a variable of type ofstream
 open the file
 Writ in the file
 close the file when access is no longer needed
(optional, but a good practice)
WRITING DATA IN THE FILES USING C++
#include <iostream>
#include <fstream>
using namespace std;
void main () {
ofstream myfile;
myfile.open("test1.txt",ios::out);
myfile << "Writing this to a file......\n";
myfile.close();
}13
WRITING DATA IN THE FILES USING C++
14
Example
• Q3: Write a program that read 4integer numbers
from the user and write them in a file .then read
the numbers from a file and print them in output
screen.
15
Input/Output with files
#include <iostream>
#include <fstream>
using namespace std;
void main(){
ofstream obj_out("file_05.txt",ios::out);
int x;
for(int i=0;i<=3;i++)
{
cin >> x;
obj_out << x << endl;
}
ifstream obj_in("file_05.txt",ios::in);
obj_in >> x;
while(!obj_in.eof())
{
cout << "X from the file is: " << x << endl;
obj_in >> x;
}
}
Input/Output with files
#include <iostream>
#include <fstream>
using namespace std;
void main(){
// To write in a file:
ofstream obj_out("file_02.txt",ios::out);
int x;
while(cin >> x)
obj_out << x << endl;
// To stop writing to a file, press CTRL+Z then ENTER
// To read from a file:
ifstream obj_in("file_05.txt",ios::in);
while(obj_in >> x)
cout << "X from the file is: " << x << endl;
}
Input/Output with files
19
Example
• Q: Write a program that copies a content of a file
into a one-dimensional array. Assume that the
content of the file is integers.
20
Solution
#include<iostream>
#include<fstream>
using namespace std;
void main()
{
int x, c=0;
ifstream fin1("d:\\test.txt", ios::in);
while(fin1>>x)
c++;
ifstream fin2("d:\\test.txt", ios::in);
int *a = new int[c];
for(int i=0; i<c; i++)
fin2 >> a[i];
// To print the content of the array
for(int i=0; i<c; i++)
cout << a[i] << endl;
}
21
• We can use the same variable from type
fstream for input and output in the same
time as the following example.
22
Input/Output with files
#include <iostream>
#include <fstream>
using namespace std;
void main(){
fstream obj("file_05.txt");
int x;
for(int i=0;i<=3;i++)
{
cin >> x;
obj << x << endl;
}
obj.close();
obj.open("file_05.txt");
obj>> x;
while(!obj.eof())
{
cout << "X from the file is: " << x << endl;
obj>> x;
}
}
Example
• Q: Write a program that copies a content of a file
into a one-dimensional array. Assume that the
content of the file is string.
24
Solution
#include<iostream>
#include<fstream>
using namespace std;
void main()
{
string x; int c=0;
ifstream fin1("test.txt");
while(fin1>>x)
c++;
fin1.close();
ifstream fin2("test.txt“); //note: we can use the same variable fin1.open("test.txt“);
string *a = new string[c];
for(int i=0; i<c; i++)
fin2 >> a[i];
// To print the content of the array
for(int i=0; i<c; i++)
cout << a[i] << endl;
}