0% found this document useful (0 votes)
21 views23 pages

FPCPP C5

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)
21 views23 pages

FPCPP C5

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/ 23

1

FUNDAMENTALS OF

5
PROGRAMMING IN C++
FILE OPERATIONS
IO-operations 2
 Input can be taken from the keyboard or from a file.
 It delivered to your program via a C++ construct known as a stream.
 Similarly, output can be sent to the screen or to a file.
 and output from your program is delivered to the output device via a stream.
 C++ standard IO is based on three classes:
 the istream class for input,
 the ostream class for output,
 and the iostream class for input/output.
 C++ refers to files as streams since it considers them a stream of bytes.
Streams 3
 A stream is a flow of characters (or other kind of data).
 If the flow is into your program, the stream is called an input stream.
 If the flow is out of your program, the stream is called an output stream.
 Example: cin is an input stream and cout is an output stream objects defined in iostream
library.
Why file? 4
• The keyboard input and screen output support temporary data.
• The data will be erased (or lost) when the program ends.
• Files provide you with a way to store data permanently.
• The content of a file will remain after the program has finished running.
• File IO supports:
• large volume of data,
• over and over again use of input data by many programs without the need to type in the data
separately for each program.
• When your program :
• takes input from a file, it is said to be reading from the file.
• sends output to a file, it is said to be writing to the file.
File IO 5
• In C++, a stream is a special kind of variable known as an object.
• Stream objects handles inputs to and outputs from a program whatever the source and destination
will be.

• If you want to use a stream to get input from a file (or give output to a file), you must
declare the stream and you must connect the stream to the file.
Declaring streams 6
• The streams cin and cout are already declared for you, but if you want a stream to connect
to a file,
• Stream must be declare just as you would declare any other variable.
• The type for input-file stream variables is named ifstream (for “input-file stream”).
• Ifstream objects used for opening file to read.
• The type for output-file stream variables is named ofstream (for “output-file stream”).
• Ofstream objects used for opening file to write.
• The type for both input and output file stream variable is named fstream (for “file stream”).
• Fstream objects used for opening file to read from and write to a file at the same time.
… 7
 Example:
ifstream readfile;
 // declaring an input-file stream object called readfile to open file to read input data into our program.

ofstream writefile;
 // declaring an output-file stream object called writefile to open file to write output data from our
program.
 The types ifstream and ofstream are defined in the library with the header file fstream,
 So to use them we must include #include<fstream> directive in the program,
 Note after declaring the stream variables(objects), we must connect them to the actual file
from our disk.
Connecting streams to a file 8
• Stream variables, such as readfile and writefile declared earlier, must each be connected to
a file.
• This is called opening the file and is done with a function named open.
• For example, suppose you want the input/output stream connected to the file named
myfile.txt.
• The connection can be done using open function,
fstream rwfile; // declaration of input-file stream.
rwfile.open(“myfile.txt”); // connecting a stream to the actual file.
• Or a constructor
fstream rwfile(“myfile.txt”); // the connection done in one line.
Opening file to read 9
• We must use ifstream object to open file as an input to( to read data for) the program.
• When we open a file using ifstream object (to read):
• The compiler will cause an error if the file we mentioned doesn’t exist in place.

ifstream readfile; or ifstream readfile (“myfile.txt”);


• After opening readfile.open(“myfile.txt”);
a file in such a manner:
• We can use >> operator to extract data into the program as a word,
• or we can just extract character by character using get() function.
… 10
readfile>>string_variable;
or char_variable =
readfile.get();
• These techniques helps us to extract data from the ifstream stream object
• word by word
• or character by character respectively.
• Note: reading a file out of bound will cause an error,
• therefore we should only read until the end of file.
• To make sure the end of file use:

• if(!readfile.eof())
Opening file to write 11
• We must use ofstream object to open file to write data (output) from the program
permanently.
• When we open a file using ofstream object (to write):
• The compiler will automatically create it, if it doesn’t exist in place.
• Or if the file exists it automatically starts overwriting it unless opened in append mode.

ofstream writefile; or writefile readfile (“myfile.txt”,


writefile.open(“myfile.txt”, AM); AM);
• After opening a file in such a manner:
• We can use << operator to write data onto the file as a string,
• or we can just write character by character using put() function.
… 12
writefile<<string_variable;
or
writefile.put(char_variable);
• These techniques helps us to write data on the file
• As a string at a time
• or character respectively.
• Every file should be closed when your program is finished getting input from the file or
sending output to the file.
• Closing a file disconnects the stream from the file. A file is closed with a call to the function
close.

• writefile.close() or readfile.close()
File Pointers
 Each file has two pointers
 one is called input pointer
 and second is output pointer.
 The input pointer is called get pointer and the output pointer is called put pointer.
 When input and output operation take places, the appropriate pointer is automatically set
according to the access mode.
 For example:
 when we open a file in reading mode, file pointer is automatically set to the start of the file.
 when we open a file in append mode, the file pointer is automatically set to the end of file

 In C++ there are some manipulators by which we can control the movement of the pointer. The
available manipulators are:
 seekg() tellp(): this gives the current position of put
 seekp() pointer (output pointer) eg. ofstream fileout;
fileout.open(“c:\\
 tellg()
test.txt”,ios::app);
 tellp() int length = fileout.tellp();
 seekg(): this moves get pointer i.e. input pointer to a specified location.
 For e.g. infile.seekg(5); move the file pointer to the byte number 5 from starting point.
 seekp(): this move put pointer (output pointer) to a specified location.
 For example: outfile.seekp(5);
 tellg(): this gives the current position of get pointer (input pointer)
Special operations in a File 15
 put() - It writes a single character to file.

 get() - It reads a single character from file.

 Note: For seekp and seekg three reference points are passed:
 ios::beg - beginning of the file
 ios::cur - current position in the file
 ios::end - end of the file
16

Tools for stream


IO
Output formatting tools 17
 The layout of a program’s output is called the format of the output.
 In C++ you can control the format with commands that determine such details as:
 the number of spaces between items
 and the number of digits after the decimal point.
 Example: for formatting decimal numbers
 cout.precision(2);
 // the function precision associated to object cout limits the number of digits to
be displayed after the decimal point.
 cout<<4.85601<<endl; // 4.85 will be displayed
 The output formatting tools can be used with any stream object in C++.
… 18
 cout.setf() and cout.unsetf() functions:
 setf() is an abbreviation for set flags.
 A flag is an instruction to do something in one of two possible ways.
 If a flag is given as an argument to setf, then the flag tells the computer to write output to that stream in
some specific way.

 Example:
 cout.setf(ios::scientific);
 cout.precision(2);
 // this makes decimal numbers to be displayed in e-notations.
 cout<<0.0004563<<":"<<endl; // 4.56e-004:
… 19
… 20
Predefined character functions 21
… 22
… 23

Demo for project:


Online Exam

You might also like