KEMBAR78
Summer25 PL2 PL2 Lecture 9-File Iinput Output | PDF | Computer File | Input/Output
0% found this document useful (0 votes)
17 views25 pages

Summer25 PL2 PL2 Lecture 9-File Iinput Output

The document outlines the principles of object-oriented programming with a focus on C++ I/O systems and file handling. It explains the concept of streams in C++, predefined streams like cin and cout, and classes for file I/O such as ifstream, ofstream, and fstream. Additionally, it provides examples of file operations and relevant functions for managing file pointers.

Uploaded by

Drakest mind
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views25 pages

Summer25 PL2 PL2 Lecture 9-File Iinput Output

The document outlines the principles of object-oriented programming with a focus on C++ I/O systems and file handling. It explains the concept of streams in C++, predefined streams like cin and cout, and classes for file I/O such as ifstream, ofstream, and fstream. Additionally, it provides examples of file operations and relevant functions for managing file pointers.

Uploaded by

Drakest mind
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Principles of object-oriented

programming Course Title: Programming Language 2


Course Code: CSC 2213

Dept. of Computer Science


Faculty of Science and Technology

Lecturer No: 9 Week No: 9 Semester: Summer 24-25

Lecturer: Md. Faruk Abdullah Al Sohan; faruk.sohan@aiub.edu


Outline

 The C++ I/O System Basics


 Introduction to File I/O
C++ Streams

 A stream is a logical device that either produces or


consumes information.
 A stream is linked to a physical device by the I/O
system.
 All streams behave in the same way even though the
actual physical devices they are connected to may
differ substantially.
C++ Streams cont.…

 Because all streams behave the same, the same I/O


functions can operate on virtually any type of
physical device.
 For example, you can use the same function that
writes to a file to write to the printer or to the screen.
 The advantage to this approach is that you need to
learn only one I/O system.
Example

IOS
Program

stream istream ostream

Device iostream
The C++ Predefined
Streams
 C++ contains several predefined streams that are
automatically opened when your program begins
execution.
 They are
 cin
 cout
 cerr
 clog.
The C++ Predefined
Streams
 cin is the stream associated with standard input.
 cout is the stream associated with standard output.
 The cerr stream is linked to standard output, and
so is clog.
 The difference between these two streams is that
clog is buffered, but cerr is not.
 Typically, cerr and clog are streams to which
program debugging or error information is written.
Classes for Stream I/O in
C++
Introduction to File I/O
 What is file?
 A file is a collection of information, usually stored
on a computer’s disk. Information can be saved to
files and then later reused.
 File names:
 All files are assigned a name that is used for
identification purposes by the operating system
and the user.
File Streams
 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.
Functions of file stream
classes
ifstream:
 Input file stream Class
 open() is a member function of the class ifstream
 Inherited functions of ifstream class, from the class
istream are › read()
› get()
› seekg()
› getline()
› tellg()
Functions of file stream
classes
ofstream:
 Output file stream Class
 open() is a member function of the class ofstream
 Inherited functions of ofstream class, from the class
ostream are
 put()
 write()
 seekp()
 tellp()
Functions of file stream
classes
 fstream:
 It supports files for simultaneous input and output.
 fstream is derived from -
› ifstream
› ofstream
› iostream
Example: File create

 This code creates a file called example.txt and


inserts a sentence into it in the same way we are
used to do with cout but using the file stream
myfile instead.
Example: File create
// basic file operations
#include <iostream>
#include <fstream>
using namespace std;

int main () {
ofstream myfile; Output:
myfile.open ("example.txt");
myfile << "Writing this to a file.\n"; [file
example.txt]
myfile.close(); Writing this to a file
return 0;
}
File

 Ifstream creates an input stream (>>).


 ofstream creates an output stream (<<).
 fstream creates both input and output stream
 There are 2 formats of file operation
1. Text format :only char data.
2. Binary format.
Text Format
#include<iostream>
#include<fstream>
#include<iostream>
#include<fstream>
using namespace
using namespace std; std;
int main()
int
{ main()
int num[ ]={5,7,11,13,19};
{ ofstream fout("test.txt");
for(int i=0;i<5;i++)
int num[ ]={5,7,11,13,19};
fout<<num[i]<<endl;
return 0;
} ofstream fout("test.txt");
for(int i=0;i<5;i++)
fout<<num[i]<<endl;
return 0;
}
get & put pointer

 Theoretically, get pointer should move as we take


input from a file & put pointer should move as we
write to a file. But practically, get & put pointers
move together.
Relevant
Functions
 Four relevant functions are-
 seekg: Moves the get pointer to a specified byte.
 seekp: Moves the put pointer to a specified byte.
 tellg: Tells the present position (byte no.) of get
pointer.
 tellp: Tells the present position (byte no.) of put
pointer.
File cont.…

 In file, counting of bytes starts from zero (0).


 Here, present position means next byte to read or
write.
 In a single file, at a time more than one file
object/pointer should not work.
Example
int main()
{
cout<<x<<endl;
ofstream fout("test2.txt");
fin.seekg(1); Output:
fout<<10<<endl;
fin>>x;
fout.close();
cout<<x<<endl; 10
ifstream fin("test2.txt");
cout<<fin.tellg()<<endl; 0
int x; 3
return 0;
fin>>x; }
Example
int main()
{
ofstream fout("test2.txt");
if(!fout)cout<<"Can't open file"<<endl;
else{ Output:
cout<<fout.tellp()<<endl;
fout<<"hello world"<<'\n';
0
cout<<fout.tellp()<<endl; 12
fout.seekp(2); 18
fout<<"and this planet"<<'\n';
cout<<fout.tellp()<<endl;
fout.close();

}
return 0;
}
Example
#include <iostream>
#include<fstream>
using namespace std;

int main()
{
ifstream fin("test2.txt"); Output:
char x;
while(!fin.eof()){
Enjoy Learning File System
fin.get(x);
cout<<x;
}
fin.close();
return 0;
}
References
1. https://www.tutorialspoint.com/cplusplus/cpp_basic_syntax.htm
2. https://www.cplusplus.com/doc/tutorial/
3. http://en.cppreference.com/w/cpp/header
Books
 Teach Yourself C++, 3rd Edition, Herbert Schildt.
 The C++ Complete Reference, 4th Edition, Herbert Schildt.
 C++ How to Program, 4th Edition, Deitel and Deitel.
 The C++ Programming Language, Special 3rd Edition, Bjarne Stroustrup
 Thinking in C++, Volume One, 2nd Edition. Bruce Eckel.

You might also like