Professional Documents
Culture Documents
Handling
PF Team, FIT UCP
PAGE
Topics Heading
PAGE
Importance
PAGE
Importance
PAGE
File
PAGE
Streams <iostream>
Stream is flow of water from one direction to another
In computer science streams are the flow of data, sometimes from CPU to Screen or other way
around.
It’s <iostream>
<iostream>
cout
CPU Screen
cin
PAGE
Files Stream <fstream>
If the data flows to and from other than screen/console, stream changes.
In case of file, stream is fstream.
<fstream>
ofstream
Text
CPU Flow of data
File
ifstream
PAGE
<iostream> <fstream>
Note: ofstream and ifstream are datatype you can make as many objects of
both types as you like. It depends upon on how many files you are working in
your program
ofstream object
ofstream objects are used for output (writing on file)
file_handling.cpp output.txt
#include <iostream> Welcome to VLE
#include <fstream> This is my first .txt file
using namespace std;
int main()
{
ofstream fout;
fout.open("output.txt");
fout<<"welcome to VLE"<<endl;
fout<<"This is my first .txt file";
fout.close();
return 0;
}
PAGE
Cont.
re-opening the same file output.txt causes the previous data loss
file_handling.cpp output.txt
#include <iostream> Hello I am doing well
#include <fstream>
using namespace std;
int main()
{
ofstream fout;
fout.open("output.txt");
fout<<“Hello I am doing well"<<endl;
fout.close();
return 0;
}
PAGE
Comparison of cout and fout
• To print number on CONSOLE screen, • To print number to a file, fout is used.
cout is used.
• cout, by default, is linked with console • fout is not linked with any file. So there is
screen. So no need to open it with cout. a need to open the file with fin.
fout.open();
<iostream> have cout and cin only for output and input while
you can make many objects of ofstream for output and many
objects of ifstream for input a file, WHY????
ofstream fout;
fout.open("output.txt");
fout<<age<<" ";
fout<<marks<<" ";
fout<<name<<" ";
fout.close();
return 0;
}
PAGE
File_handling.cpp User input data Output.txt
#include <fstream> cin>>age 32
#include <iostream> 32 99.99
int main()
{ cin>>marks Ahmed Muneeb
int age; 99.99
float marks; cin>>name
char name[50] = {‘\0’}; Ahmed Muneeb
cout<<”Enter your age”<<endl;
cin>>age;
cout<<”Enter your marks”<<endl;
cin>>marks;
cout<<”Enter your name”<<endl;
cin.ignore();
cin.getline(name, 49)//last index for\0
ofstream fout;
fout.open("output.txt");
fout<<age<<" ";
fout<<marks<<" ";
fout<<name<<" ";
fout.close();
return 0;
}
PAGE
Writing on multiple files output1.txt
Writing in file 1
#include <fstream>
int main()
{
ofstream fout1;
ofstream fout2
ofstream fout3;
output2.txt
fout.open("output1.txt");
fout.open("output2.txt"); Writing in file 2
Writing file 1
fout.open("output3.txt");
fout1.close();
fout2.close();
fout3.close();
return 0;
} output3.txt
Writing in file 3
PAGE
Points to ponder
ofstream object fout open/access a file like,
fout.open(“output.txt”)
if text file name “output.txt” does not exist, it will create the file automatically
If an existing file is open for writing, writing begins from the start of file. If the file
already have some data in it that previous data will be lost
PAGE