0% found this document useful (0 votes)
7 views14 pages

Chapter Four I-O

Chapter Four discusses file operations in C++, covering topics such as stream classes, file reading and writing modes, and types of files. It explains the use of ifstream and ofstream for input and output file streams, and details functions for managing file streams. The chapter also differentiates between sequential and random access methods for file handling.

Uploaded by

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

Chapter Four I-O

Chapter Four discusses file operations in C++, covering topics such as stream classes, file reading and writing modes, and types of files. It explains the use of ifstream and ofstream for input and output file streams, and details functions for managing file streams. The chapter also differentiates between sequential and random access methods for file handling.

Uploaded by

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

Chapter Four

File Operations (File Input/output)

Injibara University
2015 E.C

1
Outline
 Introduction

 Stream classes

 Writing and reading modes

 Writing to and reading from a file

 Types of files (Text and binary)

 File access methods (sequential and random access files)

2
Introduction
 A file is a logical collection of record.

 Standard input and output in C++ is done through the use of streams.

 Streams are generic places to send or receive data. Keyboard, screen, file, network.

 In C++, I/O is done through classes and functions found in <iostream>.

 There are two variables (among others) defined in <iostream>. cout is used for output, cin for
input.

 cout and cin are not key words in the C++ language.

 They are variables, instances of classes, that have been declared in <iostream>. cout is a
variable of type ostream. cin is a variable of type istream.
3
Cont.
 stream - a sequence of characters.

 cin - input stream associated with keyboard.

 cout - output stream associated with display.

 file (fstream)

ifstream - defines new input stream (normally associated with a file).

ofstream - defines new output stream (normally associated with a file)

 iostream -- contains basic information required for all stream I/O operations.

 fstream -- contains information for performing file I/O operations.

4
File stream classes
 ifstream: It is the input file stream class.

 Its member function open( ) associates the stream with a specified file in an input mode.

 ofstream : It is the output file stream class.

 Its member function open( ) associates the stream with a specified file in an output mode.

 fstream : It supports files for simultaneous input and output.

 It is derived from ifstream, ofstream and iostream classes.

5
Cont.
 Variables are stored in Main Memory/RAM " values are lost when program is finished
executing

 To preserve the values computed by the program: save them to a file

 Files are stored in Secondary Storage

 To have your program manipulate values stored in a file, they must be input into variables
first.

 objects of type ofstream can output (write) values to a file. (like cout).

 objects of type ifstream can input (read) values from a file. (like cin).

6
Cont.
 The functions associated with fstream are:
 open : This associates the stream with a specified file.

 close : It closes the stream.

 close all : It closes all the opened streams

 seekg : Sets current `get' position in a stream

 seekp : Sets current `put' position in a stream

 tellg : Returns the current `get' position in a stream

 tellp : Returns the current `put' position in a stream.

7
Opening a file
 To input from a file, declare an ifstream variable and open a file by its name

ifstream someFile;

someFile.open(“mydatafile.txt”);

 To output to a file, declare an ofstream variable, and open a file by its name.

ofstream anotherFile;

anotherFile.open(“myoutputfile.txt”);

 If the file “myoutputfile.txt” does not exist, it will be created.

 Stream variable is associated with the file.


8
Closing a file
 To close a file stream when you are done reading/writing:

someFile.close();

anotherFile.close();

 Not required, but good practice

9
Writing to a file
 Use the stream insertion operator: <<

10
Reading from a file
 When opened, file stream's read position points to first character in file.

 Extraction operator (>>) starts at read position and skips whitespace to read data into the
variable.

 The read position then points to whitespace after the value it just read.

11
File types
 Text files: plain text files combination of alphabets, digits and symbols.

 Examples of text files, xyz.txt, abc,doc, file.cpp

 Binary files: collection of 1s and os.

 Binary file is stored with extension.

 Example of binary file is .exe file.

12
File access methods
 Sequential access: the records are accessed sequentially.

 Time consuming process

 Random access (direct access): record is accessed directly.

 It is faster than sequential access method.

13
Advantage of pointers
 Pointers are useful in C++ for several reasons

 To allow pass-by-ref semantics (can also be done with references)

 To allow dynamic memory management.

 Pass-by-ref with pointers

14

You might also like