0% found this document useful (0 votes)
4 views

Ch6 - File Streaming PF supporting material

Uploaded by

jeffkurly
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Ch6 - File Streaming PF supporting material

Uploaded by

jeffkurly
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Programming for Engineers

Lecture 39-40

Muhammad Ismail Mangrio


Lecturer, Sukkur IBA
1
C++ Files and Streams
• We use iostream standard library,
– for cin and cout objects for reading from standard input
and writing to standard output respectively.

• This requires another standard C++ library


called fstream .

• It defines three new data types mentioned in next


slide:

2
C++ Files and Streams

Note:To perform file processing in C++, header files


<iostream> and <fstream> must be included in your C++
source file. 3
Opening a File:
• A file must be opened before you can read from it or
write to it.

• Either the ofstream or fstream stream class may be


used to open a file for writing.

• ifstream stream class is used to open a file for


reading purpose only.

4
Writing to a File
• Write information to a file from your program, using stream
insertion operator (<<) just as you use that operator to output
information to the screen.

• The only difference is that you use


an ofstream’s or fstream’s object instead of the cout object.

5
Reading from a File
• Read information from a file into your program using the
stream extraction operator (>>) just as you use that operator
to input information from the keyboard.

• The only difference is that you use


an ifstream’s or fstream’s object instead of the cin object.

6
7
Closing a File
• When a C++ program terminates,
– automatically closes flushes all the streams,
– release all the allocated memory and
– close all the opened files.

• But it is always a good practice that a programmer should


close all the opened files before program termination.

• Following is the standard syntax for close() function which is a


member of fstream, ifstream, and ofstream stream classes.

– void close();
8
Read & Write Example
#include<constream.h>
#include<fstream.h>
void main()
{ clrscr();
char data[100];
ofstream outfile;
outfile.open("test1.dat");
cout<<"Writing Data ";
cout<<"\nEnter your name : ";
cin.getline(data,100);
cout<<data<<endl; //Print data on console
outfile<<data<<endl; //print/write data on file
cout<<"\n\n Data written successfully\n\n";
outfile.close(); 9
Read & Write Example
ifstream infile; //infile instance created
infile.open("test1.dat");
cout<<"\n Reading data from the file \n";
infile>>data;
cout<<"Write data on the screen \n";
cout<<data;

cout<<"\n\nclosing opened file....";


infile.close();

getch();
10
}
Assignment
Create a file with your own name using filing.
Write your name, roll number and cnic number.
Then read data from that file on console.

11

You might also like