0% found this document useful (0 votes)
9 views28 pages

Chapter 6-File in C++

This document explains the concepts of file input and output in C++, including the definition of files, the use of streams for data flow, and the types of file streams such as ifstream, ofstream, and fstream. It outlines the steps for file processing, including opening, using, and closing files, as well as checking for successful file openings. Additionally, it discusses sequential and random access methods for file handling.
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)
9 views28 pages

Chapter 6-File in C++

This document explains the concepts of file input and output in C++, including the definition of files, the use of streams for data flow, and the types of file streams such as ifstream, ofstream, and fstream. It outlines the steps for file processing, including opening, using, and closing files, as well as checking for successful file openings. Additionally, it discusses sequential and random access methods for file handling.
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/ 28

1

Chapter 6
Input output
2

What is a File?
▶A file is a collection on information, usually stored
on a computer’s disk.
▶ Information
can be saved to files and then later
reused when it is needed.

▶ Allfiles are assigned a name that is used for


identification purposes by the operating system
and the user.
▶ Files are used to store data permanently
3

Basic Concepts: The byte Stream


▶ A stream is general name given to flow of data
▶ A stream is a sequence of byte.
▶ The source stream that provides data to programs is
called input stream.
▶ The destination stream receive output from the
program is called output stream.
▶ The classes used for input/output to the devices are
declared in the IOSTREAM file.
▶ The classes used for disk file are declared in the
FSTREAM file.
4
Cont…
5
Cont…
▶ We have been using the iostream standard
library so far, which provides cin methods for
reading from standard input.
▶ Andcout methods for writing to standard output
respectively.
6

Using C++ Stream I/O


• Default input stream is called cin
• Default output stream is called cout
• Use the extraction operator >> with cin
• Use the insertion operator << with cout
▶ To perform file processing in C++, header files
<iostream>and <fstream>and must be
included in your C ++source file.
7
fstream
▶ fstream is standard C++ library that defines three
data types:
▶ ofstream: This data type represents the output file
stream and is used to create files and to write
information to files.
▶ ifstream: This data type represents the input file
stream and is used to read information from files.
▶ fstream: This data type represents the file stream
generally, and has the capabilities of both
ofstream and ifstream which means it can create
file.
8
Cont…

Use this link for more information:


https://www.guru99.com/cpp-file-read-write-open.html
9

General File I/O Steps


• Declare a file name variable
•Associate the file name variable with the disk file
name
• Open the file
• Use the file
• Close the file
10
Cont…

 Declare a file name variable


#include <fstream>

ifstream input_filename_var; // input file


ofstream output_filename_var; // output file

 Associate the file name variable with the disk file


name and open it
input_filename_var.open(“pathname/filename”, ios::in);
output_filename_var.open(“pathname/filename”, ios::out);
11
Cont…

File opening:
▶ The file must be opened; if the file does not yet
exits, opening a file means creating it.
▶A file must be opened before you can read from
it or write to it.
▶ Either the ofstream or fstream object may be
used to open a file for writing.
▶ ifstream object is used to open a file for reading
purpose. 11
12
Cont…
Syntax for file opening
fstream fileName;
fileName.open("file.txt", ios::openmode
mode);
▶ The first argument specifies the name and
location of the file to be opened & the
second argument of the open member
function defines the mode in which the file
should be opened.
▶ Information is then saved(write) to the file,
read from the file, or both.
13
Cont…

Common mode examples:


▶ ios::in - Open a file for reading.
▶ ios::out - Open a file for writing.
▶ ios:: app
- Append mode. All output to that file
to be appended to the end.
14
Cont…

• Use the file


•Use an input file as you would use the cin input
stream
inputfile1 >> x >> y; // x and y are integers
inputfile2 >> ch; // ch is a char
ch = inputfile3.get(); // ch is a char
15
Cont…

•Use an output file as you would use the cout


output stream
outputfile1 << x << y; // x and y are integers
outputfile2 << ch; // ch is a char
outputfile3 << “Hi there!” << endl; // literal string
16
Cont…
• Close the file
input_filename_var.close();
output_filename_var.close();

 All files are closed automatically upon termination of


program execution, but it is a good habit to close
them explicitly.
 Also, close them as soon as they are no longer
needed by the program.
17
Checking that Files Have Opened
Successfully
•Always check that all files (input or output) have been
successfully opened.
•If any file cannot be opened, send a message to the user and
terminate program execution.
ifstream myFile(“inputData”);
if (!myFile) {cout<< “Input file
could not be opened” <<
endl; }

else{cout<< “Input file opened sccssfully” << endl;


}
18
Writing and reading to/from a File
• While doing C++ programming, you write information to
a file from your program using the stream insertion
operator <<just as you use that operator to output
information to the screen.
•The only difference is that you use an ofstream or fstream
object instead of the cout object.
•You 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 or fstream
object instead of the cin object.
19

Example: file writing


20

Example: File Reading


21

More example-Writing file


#include<iostream>
#include<fstream> Using
namespace std; int main(){
fstream file1; File1.open(“test.txt”,ios::out);//write in to
text file If(file1.is_open()){

file1<<“ This my file\n”;

File1.close();

return 0;}
22

More example-append

Use the following line of code to append the text on


previous text.
file1.open(“test.txt”,ios::app);//append to the end of text
file
If(file1.is_open()){
file1<<“ This your file\n”;

file1.close();
23

More example-reading file


#include<iostream> #include<fstream> #include<string> Using
namespace std;

int main(){
fstream file1;
file1.open(“test.txt”,ios::in);//reading a file
if(file1.is_open()){
string mytext;
while(getline(file1,mytext)){
cout<<mytext<<endl;
file1.close();

return 0;}
24

More…Writing from user


25
More…Reading
File Access methods 26
• Sequential access method: To reach the desired one, all file
prior to it should be traversed.
• Random access method: randomly chosen a file to get the
desired item.
Consider the following two cases
1. Opening a song from Cassette player
2. Opening a song from CD player
Which one is cheap?
Which one is fast?

Which one has larger capacity?


Which one is considered as sequential access? and random
access?
Cont… 27
• Sequential Access
It is the simplest access method. Information in
the file is processed in order, one record after
the other. This mode of access is by far the
most common; for example, editor and
compiler usually access the file in this fashion.
• In a sequential file, information is accessed in
a chronological order.
• The random access provides instant fetching
of a record and that too c an in any order or
no order at all.
File Access methods 28

Read About: Functions


• seekg()
• seekp()

You might also like