Professional Documents
Culture Documents
Introduction:
performed.
o A File is a collection of related data stored in a particular area on the disk.
o Data transfer between console unit and the program.
o Data transfer between the program and a disk.
Streams:
o Stream is a sequence of bytes.
o If use file stream as an interface between the program and files.
o We are using the iostream standard library, it provides cin and cout methods
for reading from input and writing to output respectively.
o To read and write from a file we are using the standard C++ library
called fstream.
o Let us see the data types define in fstream library is:
1.Text Stream:
o Text Stream sequence of characters.
o The stream holds the same character that we see the stream data is displayed.
o Only a few special character like new line ,tab etc.are stored according to the system.
o So there may not be direct one to one relationship between character in the device file
and character that are read and write.
2.Binary Stream:
o Binary Stream is a collection of bytes.
o Binary Stream has one to one relationship between byte in the stream and the device
file.
o The number byte in a file and number of byte in a device file are same.
o Stream can be further divided in to Input stream and Output stream.
o A program can read data from input stream.
o A program can write data from output stream.
1.iostream:
o iostream for standard input output stream.
o This header file contain definition to object like cin , cout , cerr etc,
2.iomanip:
o iomanip stand for input output manipulator. the method declared in file this file are
used for manipulating stream.
o This file contain definition of setw, set precision etc.
3.fstream:
o This data type represents the file stream generally, and has the capabilities of both ofstream and
ifstream which means it can create files, write information to files, and read information from
files.
o The two keyword cout and cin in C++are used very often for printing output and taking input
respectively .
o these two are the most basic method of taking input printing output in C++.to use cin and cout in
C++ one most include the header file iostream in the program.
#include <iostream>
int main()
{
cerr << "An error occurred";
return 0;
}
4. buffered standard error stream (clog): This is also an instance of ostream class
and used to display errors but unlike cerr the error is first inserted into a buffer
and is stored in the buffer until it is not fully filled. or the buffer is not explicitly
flushed (using flush()). The error message will be displayed on the screen too.
o File is a collection of related data stored in some storage device like Disk
,pen drive, CD etc.
o Every file stored in any device is associated with file name.
o File name is made up of name and extension.
o File means storing data in computer device.
o To process /add data in a file we need to open the file
o C++ provides set of classes ,with there functions, for handling file in our
programs.
o To open a file we create object of the required class.
o Then we open a file using some open() function of the class.
Syntax:
open (FileName,Mode);
o FileName is a string that mention full file name with its parent folder path.
o If the program and the file to open are in same folder ,then only filename is
enough.
o Mode is an int value that specifies the mode e.g read and write mode.
o The mode are predefined in ios class ,of Stream class hierarchy.
o Mode is optional ,and its value is assumed by compiler according the class used.
e.g. To open file for reading,
ifstream fin; //object if ifstream
fin.open (“mydata.txt”). //open the file
Using above classes and their function one can perform following operation on file.
o Open a file. Existing file can be open and new file can be created.
o Read, write or modify data in file
o Save data in the file
o Close the file
o
Detecting End _Of_File(EOF):
eof():while reading the content of file sequentially, one has to check whether end of file is
reached.
end of file C++ provide a functions eof()which return true (i.e. 1)if end of file is
reached,else return false(i.e. 0).
e.g.
if(!obj.eof())
{
Statements;
}
ofstreamfout;
o Each of the open member function of classes of stream ,if stream and f stream has a
default mode that is used if the file is opened without a second argument.
o The seekp() and tellp() are member function of ofstream. The seekg() or tellg() are
member function of ifstream. The class fstream deals with file in both input and output
modes.
o Hence, there are two file pointer in class fstream and the put pointer used for writing and
get pointer used for the reading.
1. seekg():
o The function is used to position the get-pointer at given distance from given starting
point.
Syntax:
seekg(int offset,int start);
2. seekp():
o The function is used to place the put-pointer at the given distance from string pointer.
3. tellg():
o the function is used to get the current position of get pointer.
Syntax:
int tellg();
e,g. int p = obj.tellg();
4. tellp():
o the function is used to get the current position of put pointer.
Syntax:
Int tellp();
e,g. int p =obj.tellp();
o for working with ‘get’ pointer(for seekg and tellg)file must have read mode.
o for working with ‘put’ pointer(for seekp and tellp)file must have write mode.
setiosflags()manipulator:
<<var;
o Note that manipulator affect only the data follows them in the stream i.e. formatting
effects to the I/O that is coming after they are used in the code ,not the data that come
before them.
o Few of the I/O manipulator are as below.
Functions Use
setw() Set field width for output.
setprecision() Set precision(number of digit displayed)
setfill() Set fill character for output (default is a space)
setiosflags() Set specified flags
resetiosflags() Clear specified flags
Updating File:
consider a text field has content :test1.txt
abc
123
Pqr
456
Stu
789
Lmn
012
And you want to update “stu” number from 789 to 276.
#include<iostream>
#include<fstream>
#include<string>
#include<cstdio>
int main()
{
string x;
ifstream file ("test1.txt");
ofstream ofile ("test2.txt");
cout<<remove<<endl;
while(!file.eof())
{
getline(file,x);
if(x=="789")
{
ofile<<"276"<<endl;
}
else
{
ofile<<x<<endl;
}
}
file.close();
ofile.close();
remove("test1.txt");
return 0;
}
Out Put:
abc
123
Pqr
456
Stu
276
Lmn
012