Introduction
Streams in C++
File Modes
Best Practices
File Pointer Movements
Memory pointers in file handling
File types for storing data
L23 - File streams - Text files
Department of Computer Science
University of Pretoria
09 April 2025
COS132 File types for storing data 1 / 19
Introduction
Streams in C++
File Modes
Best Practices
File Pointer Movements
Memory pointers in file handling
Text File Handling allows data storage on secondary
storage for future retrieval.
Data stored in a text file is treated as text (even
numbers).
Useful for storing data between program runs.
COS132 File types for storing data 2 / 19
Introduction
Streams in C++
File Modes
Syntax for File Operations
Best Practices
File Pointer Movements
Memory pointers in file handling
C++ provides three stream classes for file handling:
ofstream - Output file stream (Write to file)
ifstream - Input file stream (Read from file)
fstream - For both input and output
COS132 File types for storing data 3 / 19
Introduction
Streams in C++
File Modes
Syntax for File Operations
Best Practices
File Pointer Movements
Memory pointers in file handling
Writing to a file:
ofstream o u t f i l e ;
o u t f i l e . open ( ” d a t a . t x t ” ) ;
o u t f i l e << ” H e l l o F i l e ! ” ;
outfile . close ();
Reading from a file:
ifstream i n f i l e ;
i n f i l e . open ( ” d a t a . t x t ” ) ;
i n f i l e >> v a r i a b l e ;
i n f i l e . close ();
COS132 File types for storing data 4 / 19
Introduction
ofstream Example
Streams in C++
ifstream Example
File Modes
fstream Example
Best Practices
Example Program 1
File Pointer Movements
Example Program 2
Memory pointers in file handling
Mode Description
ios::in Open file for reading.
ios::out Open file for writing. Overwrites existing content.
ios::app Append new data to end of file.
ios::in — ios::out Open file for both reading and writing.
COS132 File types for storing data 5 / 19
Introduction
ofstream Example
Streams in C++
ifstream Example
File Modes
fstream Example
Best Practices
Example Program 1
File Pointer Movements
Example Program 2
Memory pointers in file handling
#i n c l u d e <f s t r e a m >
#i n c l u d e <i o s t r e a m >
u s i n g namespace s t d ;
i n t main ( ) {
ofstream o u t f i l e ;
o u t f i l e . open ( ” d a t a . t x t ” ) ;
o u t f i l e << ”Welcome t o F i l e H a n d l i n g ! ” << e n d l ;
o u t f i l e << 2025 << e n d l ;
outfile . close ();
}
COS132 File types for storing data 6 / 19
Introduction
ofstream Example
Streams in C++
ifstream Example
File Modes
fstream Example
Best Practices
Example Program 1
File Pointer Movements
Example Program 2
Memory pointers in file handling
#i n c l u d e <f s t r e a m >
#i n c l u d e <i o s t r e a m >
u s i n g namespace s t d ;
i n t main ( ) {
ifstream i n f i l e ;
i n f i l e . open ( ” d a t a . t x t ” ) ;
string text ;
while ( g e t l i n e ( i n f i l e , text )) {
c o u t << t e x t << e n d l ;
}
i n f i l e . close ();
}
COS132 File types for storing data 7 / 19
Introduction
ofstream Example
Streams in C++
ifstream Example
File Modes
fstream Example
Best Practices
Example Program 1
File Pointer Movements
Example Program 2
Memory pointers in file handling
#i n c l u d e <f s t r e a m >
#i n c l u d e <i o s t r e a m >
u s i n g namespace s t d ;
i n t main ( ) {
fstream f i l e ;
f i l e . open ( ” d a t a . t x t ” , i o s : : i n | i o s : : o u t | i o s : : app ) ;
f i l e << ” Adding more d a t a . ” << e n d l ;
f i l e . close ();
}
COS132 File types for storing data 8 / 19
Introduction
ofstream Example
Streams in C++
ifstream Example
File Modes
fstream Example
Best Practices
Example Program 1
File Pointer Movements
Example Program 2
Memory pointers in file handling
Writing Numbers to a File:
#i n c l u d e <f s t r e a m >
#i n c l u d e <i o s t r e a m >
u s i n g namespace s t d ;
i n t main ( ) {
ofstream o u t f i l e ;
o u t f i l e . open ( ” numbers . t x t ” ) ;
f o r ( i n t i =10; i <=100; i +=10)
o u t f i l e << i << e n d l ;
outfile . close ();
}
COS132 File types for storing data 9 / 19
Introduction
ofstream Example
Streams in C++
ifstream Example
File Modes
fstream Example
Best Practices
Example Program 1
File Pointer Movements
Example Program 2
Memory pointers in file handling
Reading Numbers from a File:
#i n c l u d e <f s t r e a m >
#i n c l u d e <i o s t r e a m >
u s i n g namespace s t d ;
i n t main ( ) {
ifstream i n f i l e ;
i n t number ;
i n f i l e . open ( ” numbers . t x t ” ) ;
w h i l e ( i n f i l e >> number )
c o u t << number << e n d l ;
i n f i l e . close ();
}
COS132 File types for storing data 10 / 19
Introduction
Streams in C++
File Modes
Best Practices
File Pointer Movements
Memory pointers in file handling
Good Practices for File Handling
Always close files after use.
Check if file opened successfully:
i f (! outfile ) {
c o u t << ” E r r o r o p e n i n g f i l e ! ” ;
}
Use file paths correctly for different operating systems.
Avoid overwriting important data accidentally.
COS132 File types for storing data 11 / 19
Introduction
Streams in C++
File Modes
Best Practices
File Pointer Movements
Memory pointers in file handling
2. Using the is open() Function
The is open() function returns:
true - if the file was opened successfully.
false - if the file failed to open or if it is in a state of error.
ofstre am m y f i l e (” example . t x t ” ) ;
i f (! my file . is open ()) {
c o u t << ” E r r o r o p e n i n g t h e f i l e . ” << e n d l ;
return 1;
}
COS132 File types for storing data 12 / 19
Introduction
Streams in C++
File Modes
Best Practices
File Pointer Movements
Memory pointers in file handling
File Pointer Movement functions in C++ are used in file
handling to control where in the file reading or writing
should happen.
When you open a file in C++, there is an internal ”file
pointer” that keeps track of the current read or write
position in the file.
Functions for moving file pointers:
seekg() - Move get pointer for reading.
seekp() - Move put pointer for writing.
tellg() - Get current position of get pointer.
tellp() - Get current position of put pointer.
COS132 File types for storing data 13 / 19
Introduction
Streams in C++
File Modes
Best Practices
File Pointer Movements
Memory pointers in file handling
seekg() Syntax
stream object . seekg ( number of bytes , f r o m p o s i t i o n ) ;
Where:
number of bytes - Bytes to move pointer.
from position - ios::beg, ios::cur, ios::end
COS132 File types for storing data 14 / 19
Introduction
Streams in C++
File Modes
Best Practices
File Pointer Movements
Memory pointers in file handling
tellg() and tellp() Syntax
stream object . t e l l g ();
stream object . t e l l p ();
Returns the current position of the file pointer.
COS132 File types for storing data 15 / 19
Introduction
Streams in C++
File Modes
Best Practices
File Pointer Movements
Memory pointers in file handling
Example: File Pointer Movement
1 #i n c l u d e <i o s t r e a m >
2 #i n c l u d e <f s t r e a m >
3 u s i n g namespace s t d ;
4 i n t main ( ) {
5 fstream fs ;
6 c h a r s [ ] = ” I am l e a r n i n g F i l e H a n d l i n g u s i n g C++”;
7 char c ;
8 f s . open ( ” d a t a . t x t ” , i o s : : i n | i o s : : o u t ) ;
9 i f ( ! f s ) { c o u t << ” Cannot open f i l e ” ; }
10 else {
11 f s << s ;
12 f s . s e e k g ( 5 , i o s : : beg ) ;
13 f o r ( i n t i =1; i <=12; i ++) {
14 f s . get ( c ) ;
15 c o u t << c ; }
16 }
17 fs . close ();
18 }
COS132 File types for storing data 16 / 19
Introduction
Streams in C++
File Modes
Best Practices
File Pointer Movements
Memory pointers in file handling
A file [Link] is opened for both reading and writing using
fstream (line 8).
The string "I am learning File Handling using C++" is
written into the file (line 11).
The function seekg(5, ios::beg) moves the get pointer to the
6th character from the beginning (0-based index) (line 12).
The for loop reads 12 characters starting from that position
using [Link](c) (lines 13 -14).
The read characters are displayed on the console using cout.
(line 15)
Finally, the file is closed using [Link](). (line 17)
Output on Console:
learning Fil
COS132 File types for storing data 17 / 19
Introduction
Streams in C++
File Modes
When are Memory Pointers Useful in File Handling
Best Practices
File Pointer Movements
Memory pointers in file handling
1. Writing Data Stored in a Pointer to a File: Sometimes data is processed
in memory (dynamically allocated), and you may want to write it to a file.
Example: Writing Pointer Data to a File
1 #i n c l u d e <i o s t r e a m >
2 #i n c l u d e <f s t r e a m >
3 #i n c l u d e < c s t r i n g >
4 u s i n g namespace s t d ;
5
6 i n t main ( ) {
7 ofstream f i l e ( ” output . t x t ” ) ;
8 c h a r * d a t a = new c h a r [ 2 0 ] ;
9 s t r c p y ( data , ” H e l l o F i l e H a n d l i n g ” ) ;
10
11 f i l e << d a t a ;
12 d e l e t e [ ] d a t a ; // F r e e memory
13 f i l e . close ();
14 }
COS132 File types for storing data 18 / 19
Introduction
Streams in C++
File Modes
When are Memory Pointers Useful in File Handling
Best Practices
File Pointer Movements
Memory pointers in file handling
2. Dynamic Arrays in File Processing
When the size of data is not known in advance.
Example: Reading an unknown number of student
names from a file.
In such cases, we use:
Pointers
Dynamic arrays [This topic will be covered in more
detail later in this module.]
COS132 File types for storing data 19 / 19