You are on page 1of 17

File

Handling
PF Team, FIT UCP

PAGE
Topics Heading

 Writing to a Text File


 <iostream> <fstream>
 Basic file operations: open and Lecture
close Outline
 File stream: ofstream
 Write (out mode) on one or more
files

PAGE
Importance

• Saves Data permanently


• Saves time

PAGE
Importance

 Programs outputs are temporary shown on console and vanishes as soon as


console closes
 Sometimes programs outputs need to be store either for future reference or it
becomes the input to other program for further processing
 Instead of noting the output and then entering it as an input of other program it
is not time consuming, probably wasting and even not possible for huge data

PAGE
File

 A set of data stored on a computer, often on a disk drive


 Programs can read from, write to files
 Used in many applications:
 Word processing
 Databases
 Spreadsheets

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>

Output writing on screen Writing on file


Cout ofstream object (variable)
Operator Operator
<< <<
Input reading from screen
Reading from file
Cin
Operator ifstream object (variable)
>> Operator
>>

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 is pre-defined keyword. So no need • fout is user-defined keyword. So we need


to declare it. Like int, float, endl, return to declare it like we declare a variable.
etc. ofstream fout;

• 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();

• Insertion operator is used << • Insertion operator is used <<


cout << variable_name; fout << variable_name;
What will happen when the output.txt open again again and
write different sentences in it, like one you have written
“welcome to VLE” ?

<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????

Think of an example where filing is inevitable?


Writing variables and user input (cin) on File
File_handling.cpp User input data output.txt
#include <fstream> cin>>age 32
#include <iostream>
int main() 32 99.99
{ cin>>marks Ahmed
int age; 99.99
float marks; cin>>name
char name[50] = {‘\0’};
cout<<”Enter your age”<<endl; Ahmed
cin>>age;
cout<<”Enter your marks”<<endl;
cin>>marks;
cout<<”Enter your name”<<endl;
cin>>name;

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<<”writing in file 1”<<endl;


fout2<<”writing in file 2” <<endl;
fout3<<”writing in file 3” <<endl;

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

You might also like