0% found this document useful (0 votes)
32 views

CH - 5 Files (C++)

c++ course

Uploaded by

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

CH - 5 Files (C++)

c++ course

Uploaded by

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

CHAPTER 5: FILE MANAGEMENT

File handling is an important part of all programs. Most of the applications will have their
own features to save some data to the local disk and read data from the disk again. Files
which are on the secondary storage device are called physical files. In order to process
file through program, logical file must be created on the RAM. This logical file is nothing
but an object having file data type. As an object there should be a variable identifier that
points to it. This variable is called file variable and sometimes also called file handler. C+
+ File I/O classes simplify such file read/write operations for the programmer by
providing easier to use classes.

5.1. Streams and Files


The I/O system supplies a consistent interface to the C++ programmer independent of the
actual device being accessed. This provides a level of abstraction between the
programmer and the device. This abstraction is called stream. The actual device is called
a file.

5.1.1. Streams
The C++ file system is designed to work with a wide variety of devices, including
terminals, disk drives, and tape drives. Even though each device is very different, the C+
+ file system transforms each into a logical device called stream. There are two types of
streams: text and binary.
a. Text Streams
A text stream is a sequence of characters. In a text stream, certain character translations
may occur as required by the host environment. For example a new line may be
converted to a carriage return/linefeed pair. There may not be a one-to-one relationship
between the characters that are written (or read) and those on the external device.
Because of possible transformations, the number of characters written (or read) may not
be the same as those on the external device.

b. Binary streams
A binary stream is a sequence of bytes with a one-to-one correspondence to those in the
external device i.e., no character translations occur. The number of bytes written (or
read) is the same as the number on the external device. However, an implementation-

-1-
CHAPTER 5: FILE MANAGEMENT

defined number of null bytes may be appended to a binary stream. These null bytes might
be used to pad the information so that it fills a sector on a disk.

5.2. Files

In C++, a file can be anything from a disk file to a terminal or printer. You associate a
stream with a specific file by performing an open operation. Once a file is open,
information can be exchanged between it and a program. All streams are the same but all
files are not. If the file can support position requests, opening that file also initializes the
file position indicator to the start of the file. As each character is read from or written to
the file, the position indicator is incremented. You disassociate a file from a specific
stream with a close operation. If you close a file opened for output, then contents, if any,
of its associated stream are written to the external device. -- this process is referred to as
flushing the stream. All files are closed automatically when the program terminates
normally. Files are not closed when a program terminates abnormally. Each stream that is
associated with a file has a file control structure of type FILE. This structure FILE is
defined in the header stdio.h.
The File Pointer
A file pointer is a pointer to information that defines various things about the file,
including its name, status, and the current t position of the file. In essence, the file pointer
identifies a specific disk file and is used by the associated stream to direct the operation
of the I/O functions. A file pointer is a pointer variable of type FILE.
FILE * fp;

5.2.1. The standard streams


When ever a program starts execution, three streams are opened automatically.
stdin --- standard input.
stdout -- standard output
stderr -- standard error
Normally, these streams refer to the console. Because the standard streams are file
pointers, they can be used by the ANSI C file system to perform I/O operations on the
console.

-2-
CHAPTER 5: FILE MANAGEMENT

5.2.1.1. C++ File I/O Classes and Functions

To perform file I/O, the header file fstream.h is requied. fstream.h defines several classes,
including ifstream, ofstream, and fstream. These classes are derived form istream and
ostream, repectively. istream and ostream are derived form ios.
Three file I/O classes are used for File Read/Write operations:
a. ifstream - Can be used for File read/input operations
b. ofstream - Can be used for File write/output operations
c. fstream - Can be used for both read/write c++ file I/O operations
These classes are derived directly or indirectly from the classes istream, and ostream. We
have already used objects whose types were these classes: cin is an object of class istream
and cout is an object of class ostream. Therefore, we have already been using classes that
are related to our file streams. And in fact, we can use our file streams the same way we
are already used to use cin and cout, with the only difference that we have to associate
these streams with physical files. Let's see an example:

5.2.2. Text and Binary Files


In file processing, files are generally classified into two as
 Text file and
 Binary file
Text file is a file in which its content is treated as a sequence of characters and can be
accessed sequentially. Where as binary file is a file in which its content is treated as
record sequence in a binary format. Binary format refers to the actual format the data is
going to be placed and processed in the memory which is directly related to its data type.
For example, the value int count 321 will be stored in three byte if it is written in text file
considering the digit sequence ‘3’, ‘2’, ‘1’. It will be stored in two byte if it is written in
binary file since int requires two byte to store any of its value. When you open the binary
file you will see the character equivalence of the two bytes.
321 in binary equals 0000 0001 0100 0001
The first byte holds the character with ASCII value equals to one and the second byte a
character with ASCII value equals 65 which is ‘A’. Then if you open the binary file you
will see these characters in place of 321.

-3-
CHAPTER 5: FILE MANAGEMENT

5.2.3. Text File processing


File processing involves the following major steps
1. Declaring file variable identifier
2. Opening the file
3. Processing the file
4. Closing the file when process is completed.

5.2.3.1. Opening and Closing a file


An open file is represented within a program by a stream object and any input or output
operation performed on this stream object will be applied to the physical file associated to
it. In C++, you open a file by linking it to a stream. Before you can open a file, you must
first obtain a stream. There are three types of streams: input, output, and input/output. To
create an input stream, you must declare the stream to be of class ifstream. To create an
output stream, you must declare it as class ofstream. Streams that will be performing
both input and output operations must be declared as class fstream.
ifstream in ; //input stream
ofstream out ; // output stream
fstream io ; // input and output
Once you have declared a stream, you associate it with a file by using the method open().
The method open ( ) is a member of each of the three stream classes. Its prototype is:
void open (const char *filename, int mode, int access = filebuf::penprot );
Where:
Filename is the name of the file.
The value of the mode determines how the file is opened. It must be one (or more) of
these values:
Mode Description

ios::app Write all output to the end of the file

ios::ate Open a file for output and move to the end of the file (normally used to append
data to a file). Data can be written anywhere in the file.

ios::binary Cause the file to be opened in binary mode.


ios::in Open a file for input
ios::nocreate
If the file does not exist, the open operation fails.

-4-
CHAPTER 5: FILE MANAGEMENT

ios::noreplac
e If the file exists, the open operation fails.
ios::out Open a file for output
ios:trunc
Discard the file's content if it exists (this is also the default action ios::out)

You can combine two or more of these values by using them together.
ofstream out ;
out.open ( "test", ios::out); // correct statement
ofstream out1 ;
out.open ( " test"); // the default value of mode is ios::out –
// correct statment
To open a stream for input and output, you must specify both the ios::in and the ios::out
mode values. (Noe default value for mode is supplied in this case.)
fstream myStream;
myStream.open ( "test", ios::in | ios::out );
If open ( ) fails, myStrream will be zero
if (myStream){
cout << "Cannot open a file.\n";
// handle error
}
Each one of the open() member functions of the classes ofstream, ifstream and fstream
has a default mode that is used if the file is opened without a second argument:
class default mode parameter
ofstream ios::out
ifstream ios::in
fstream ios::in | ios::out
For ifstream and ofstream classes, ios::in and ios::out are automatically and respectively
assumed, even if a mode that does not include them is passed as second argument to the
open() member function. The default value is only applied if the function is called
without specifying any value for the mode parameter. If the function is called with any
value in that parameter the default mode is overridden, not combined.
File streams opened in binary mode perform input and output operations independently of
any format considerations. Non-binary files are known as text files, and some translations
may occur due to formatting of some special characters (like newline and carriage return
characters).

-5-
CHAPTER 5: FILE MANAGEMENT

Since the first task that is performed on a file stream object is generally to open a file,
these three classes include a constructor that automatically calls the open() member
function and has the exact same parameters as this member. Therefore, we could also
have declared the previous myfile object and conducted the same opening operation in
our previous example by writing:
ofstream myfile ("example.bin", ios::out | ios::app | ios::binary);
Combining object construction and stream opening in a single statement. Both forms to
open a file are valid and equivalent.
To check if a file stream was successful opening a file, you can do it by calling to
member is_open() with no arguments. This member function returns a bool value of true
in the case that indeed the stream object is associated with an open file, or false
otherwise:
if (myfile.is_open()) { /* ok, proceed with output */ }
ifstream myStream ( "myfile" ); // open file for input
When we are finished with our input and output operations on a file we shall close it so
that its resources become available again. In order to do that we have to call the stream's
member function close(). This member function takes no parameters, and what it does is
to flush the associated buffers and close the file:
myfile.close();
Once this member function is called, the stream object can be used to open another file,
and the file is available again to be opened by other processes.
In case that an object is destructed while still associated with an open file, the destructor
automatically calls the member function close(). The close method takes no parameters
and returns no value.

5.2.3.2. Reading and writing text files


Simply use the << and >> operators in the same way you do when performing console
I/O except that instead of using cin and cout, you substitute a stream that is linked to a
file.
ofstream out ("inventory");
out <<"Radios" << 39.95 << endl;
out << "Toastors" << 19.95 << endl;
out.close ( );

-6-
CHAPTER 5: FILE MANAGEMENT

Example: Basic file operations|


#include <iostream>
#include <fstream>
using namespace std;
int main () {
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
myfile.close();
return 0;
}
This code creates a file called example.txt and inserts a sentence into it in the same way
we are used to do with cout, but using the file stream myfile instead.
Example 2: writing on a text file
#include <iostream>
#include <fstream>
using namespace std;

int main () {
ofstream myfile ("example.txt");
if (myfile.is_open())
{
myfile << "This is a line.\n";
myfile << "This is another line.\n";
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
Example 3: reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while (! myfile.eof() )
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
}

-7-
CHAPTER 5: FILE MANAGEMENT

else cout << "Unable to open file";

return 0;
}
This last example reads a text file and prints out its content on the screen. Notice how we
have used a new member function, called eof() that returns true in the case that the end of
the file has been reached. We have created a while loop that finishes when indeed
myfile.eof() becomes true (i.e., the end of the file has been reached).
Checking state flags
In addition to eof(), which checks if the end of file has been reached, other member
functions exist to check the state of a stream (all of them return a bool value):
Function Description
bad() Returns true if a reading or writing operation fails. For example in the
case that we try to write to a file that is not open for writing or if the
device where we try to write has no space left.
fail() Returns true in the same cases as bad(), but also in the case that a
format error happens, like when an alphabetical character is extracted
when we are trying to read an integer number.
eof() Returns true if a file open for reading has reached the end.
good() It is the most generic state flag: it returns false in the same cases in
which calling any of the previous functions would return true.
In order to reset the state flags checked by any of these member functions we have just
seen we can use the member function clear(), which takes no parameters.
get and put stream pointers
All I/O streams objects have, at least, one internal stream pointer:
 ifstream, like istream, has a pointer known as the get pointer that points to the
element to be read in the next input operation.
 ofstream, like ostream, has a pointer known as the put pointer that points to the
location where the next element has to be written.
 Finally, fstream, inherits both, the get and the put pointers, from iostream (which
is itself derived from both istream and ostream).

-8-
CHAPTER 5: FILE MANAGEMENT

These internal stream pointers that point to the reading or writing locations within a
stream can be manipulated using the following member functions:
tellg() and tellp()
These two member functions have no parameters and return a value of the member type
pos_type, which is an integer data type representing the current position of the get stream
pointer (in the case of tellg) or the put stream pointer (in the case of tellp).
seekg() and seekp()
These functions allow us to change the position of the get and put stream pointers. Both
functions are overloaded with two different prototypes. The first prototype is:
seekg ( position );
seekp ( position );

Using this prototype the stream pointer is changed to the absolute position position
(counting from the beginning of the file). The type for this parameter is the same as the
one returned by functions tellg and tellp: the member type pos_type, which is an integer
value.
The other prototype for these functions is:
seekg ( offset, direction );
seekp ( offset, direction );

Using this prototype, the position of the get or put pointer is set to an offset value relative
to some specific point determined by the parameter direction. offset is of the member
type off_type, which is also an integer type. And direction is of type seekdir, which is an
enumerated type (enum) that determines the point from where offset is counted from, and
that can take any of the following values:
ios::beg offset counted from the beginning of the stream
ios::cur offset counted from the current position of the stream pointer
ios::end offset counted from the end of the stream
The following example uses the member functions we have just seen to obtain the size of
a file:
Example: obtaining file size
#include <iostream>
#include <fstream>
using namespace std;
int main () {
long begin,end;

-9-
CHAPTER 5: FILE MANAGEMENT
ifstream myfile ("example.txt");
begin = myfile.tellg();
myfile.seekg (0, ios::end);
end = myfile.tellg();
myfile.close();
cout << "size is: " << (end-begin) << " bytes.\n";
return 0;
}

5.2.4. Binary File processing


In binary files, to input and output data with the extraction and insertion operators (<<
and >>) and functions like getline is not efficient, since we do not need to format any
data, and data may not use the separation codes used by text files to separate elements
(like space, newline, etc...).
File streams include two member functions specifically designed to input and output
binary data sequentially: write and read. The first one (write) is a member function of
ostream inherited by ofstream. And read is a member function of istream that is inherited
by ifstream. Objects of class fstream have both members. Their prototypes are:

write ( memory_block, size );


read ( memory_block, size );

Where memory_block is of type "pointer to char" (char*), and represents the address of
an array of bytes where the read data elements are stored or from where the data elements
to be written are taken. The size parameter is an integer value that specifies the number of
characters to be read or written from/to the memory block.
There are two ways to write and read binary data to and from a file.
 get ( ) and put ( )
 read ( ) and write ( )
If you will be performing binary operations on a file, be sure to open it using the
ios::binary mode specifier.

5.2.4.1. get ( ) and put ( )


These functions are byte-oriented.
o get ( ) will read a byte of data.
o put ( ) will write a bye of data.
The get ( ) method has many forms.
istream & get( char & ch );
ostream & put ( char ch);

- 10 -
CHAPTER 5: FILE MANAGEMENT

The get ( ) method read a single character from the associated stream and puts the value
in ch, and returns a reference to the stream. The put ( ) method writes ch to the stream
and returns a reference to the stream.
char in;
ifstream in ( "test", ios::in | ios::binary);
if (!in){
cout <<"Cannot open file";
return 1;
}
while (in) //inn will be 0 when eof is reached
{ in.get ( ch );
cout << ch;
}
When the end-of-file is reached, the stream associated with the file becomes zero.
ofstream out ( "chars", io::out | ios::binary);
for (int i= 0; i < 256; i++)
out.put ( (char ) i ) ; //write all characters to disk
out.close ( );

5.2.4.2. read ( ) and write ( )


The read ( ) method reads num bytes from the associated stream, and puts them in a
memory buffer (pointed to by buf).
istream & read ( unsigned char * buf, int num );

The write ( ) method writes num bytes to the associated stream from the memory buffer
(pointed to by buf).
ostream & write ( const unsigned char * buf, int num );

If the end-of-file is reached before num characters have been read, then read ( ) simply
stops, and the buffer contains as many characters as were available. You can find out how
many characters have been read by using another member function, called gcount ( ),
which has the prototype:
int gcount ( );

5.2.4.3. More get ( ) functions


The method get ( ) is overloaded in several ways.

istream &get (char *buf, int num, char delim = '\n');

This get ( ) method reads characters into the array pointed to by the buf until either num
characters have been read, or the character specified by delim has been encountered. The

- 11 -
CHAPTER 5: FILE MANAGEMENT

array pointed to by buf will be null terminated by get ( ). If the delimiter character is
encountered in the input stream, it is not extracted. Instead, it remains in the stream until
the next input operation.

a. int get ( )
It returns the next character from the stream. It returns EOF if the end of file is
encountered.
b. getline ( )
istream & getline ( char *buf, int num, char delim ='\n');

This method is virtually identical to the get ( buf, num, delim) version of get ( ). The
difference is getline ( ) reads and removes the delimiter from the input stream.

Example: reading a complete binary file


#include <iostream>
#include <fstream>
using namespace std;

ifstream::pos_type size;
char * memblock;

int main () {
ifstream file ("example.txt", ios::in|ios::binary|ios::ate);
if (file.is_open())
{
size = file.tellg();
memblock = new char [size];
file.seekg (0, ios::beg);
file.read (memblock, size);
file.close();

cout << "the complete file content is in memory";

delete[] memblock;
}
else cout << "Unable to open file";
return 0;
}

In this example the entire file is read and stored in a memory block. Let's examine how
this is done:

First, the file is open with the ios::ate flag, which means that the get pointer will be
positioned at the end of the file. This way, when we call to member tellg(), we will
directly obtain the size of the file. Notice the type we have used to declare variable size:
ifstream::pos_type size;

- 12 -
CHAPTER 5: FILE MANAGEMENT

ifstream::pos_type is a specific type used for buffer and file positioning and is the type
returned by file.tellg(). This type is defined as an integer type, therefore we can conduct
on it the same operations we conduct on any other integer value, and can safely be
converted to another integer type large enough to contain the size of the file. For a file
with a size under 2GB we could use int:
int size;
size = (int) file.tellg();

Once we have obtained the size of the file, we request the allocation of a memory block
large enough to hold the entire file:
memblock = new char[size];

Right after that, we proceed to set the get pointer at the beginning of the file (remember
that we opened the file with this pointer at the end), then read the entire file, and finally
close it:
file.seekg (0, ios::beg);
file.read (memblock, size);
file.close();

At this point we could operate with the data obtained from the file. Our program simply
announces that the content of the file is in memory and then terminates.

Function Description
Detecting EOF It returns nonzero when the end of the file has been reached;
int eof ( ); otherwise it returns zero.
Reading and discarding characters Reads and discards characters until either num characters have
from the input stream. been nignored (1 by default ) or until the charcter specified by
istream & ignore (int num = 1, int delim is encounterdd (EOF by default). If the delimiting
delim = EOF); character is encountered, it is not removed from the input
stream.
Obtain the next character in the input One can obtain the next character in the input stream without
stream without removing it from that removing it from that stream by using peek ( ). It returns the
stream next character in the stream or EOF if the end of file is
int peek ( ); encountered.

One can return the last character read from a stream to that

- 13 -
CHAPTER 5: FILE MANAGEMENT
istream & putback ( char c); stream using putback ( ).

Forcing data to be physically written to When the output is performed, data is not necessarily
the disk immediately written to the physical device linked to the
stream. Instead, information is stored in an internal buffer until
ostream & flush ( ); the buffer is full. Only then are the contents of that buffer
written to disk. However, you can force the information to be
physically written to the disk before the buffer is full by
calling flush ( ).

5.2.5. Random Access Files


In C++'s I/O system, you perform random access by using seekg ( ) and seekp ( )
methods.
istream *seekg (streamoff offset, sek_dir origin);
ostream & seekp ( streamoff offet, seek_dir origin);

Here, streamoff is a type defined in iostream.h that is capable of containing the largest
valid value that offset can have. Also seed-dir is an enumeration that has these values:
ios::beg
ios::cur
ios::end

The C++ I/O system manages two pointers associated with a file. One is the get pointer,
which specifies where in then file the next input operation will occur. The other is the put
pointer, which specifies where in the file the next output operation will occur. Each time
an input or output operation takes place the appropriate pointer is automatically
sequentially advanced. The seekg ( ) method moves the associated file's current get
pointer offset number of bytes from the specified origin, which must be one of three
values. The seekp ( ) method moves the associated file's current put pointer offset number
of bytes from the specified origin, which must be one of three values.

5.2.5.1. Obtaining the Current File Position


You can determine the current position of each file pointer by using these methods.
streampos tellg ( );
streampows tellp ( );

- 14 -
CHAPTER 5: FILE MANAGEMENT

Here, streampos is a type defined in iostream.h that is capable of holding the largest value
that either function can return.

5.2.5.2. I/O Status


The C++ I/O system maintains status information about the outcome of each I/O
operation. The current state of the I/O system is held in an integer, in which the following
flags are encoded.

 eofbit -- 1 when end-of-file is encountered; 0 otherwise


 failbit -- 1 when a (possibly) nonfatal I/O error has occurred; 0 otherwise
 badbit -- 1 when a fatal I/O error has ocurred; 0 otherwise

These flags are enumerated inside ios. Also defined in ios is goodbit, which has the value
0. There are two ways in which you can obtain I/O status information.

a. Use the rdstate function/method.

int rdstate ( );
rdstate function returns the current status of the error flags encoded into an integer. It
returns zero, when no error has occurred. Otherwise, an error bit is turned on.

b. Use one or more of these methods.

Method Description
int bad ( ) Returns true if badbit is set.
int fail ( ) Returns true if failbit is set.
int eof ( ) Returns true if there are no errors.
int good ( ) Otherwise they return false.
Once an error has occurred, it may need to be cleared before your program continues. to
do this, use the clear ( ) method.

void clear ( int flags = 0);


If flag = zero (as it is by default), all error flags are cleared (reset to zero). Otherwise, set
flags to the flags or values you want to clear.

- 15 -
CHAPTER 5: FILE MANAGEMENT

5.2.6. Buffers and Synchronization


When we operate with file streams, these are associated to an internal buffer of type
streambuf. This buffer is a memory block that acts as an intermediary between the stream
and the physical file. For example, with an ofstream, each time the member function put
(which writes a single character) is called, the character is not written directly to the
physical file with which the stream is associated. Instead of that, the character is inserted
in that stream's intermediate buffer.

When the buffer is flushed, all the data contained in it is written to the physical medium
(if it is an output stream) or simply freed (if it is an input stream). This process is called
synchronization and takes place under any of the following circumstances:

 When the file is closed: before closing a file all buffers that have not yet been
flushed are synchronized and all pending data is written or read to the physical
medium.

 When the buffer is full: Buffers have a certain size. When the buffer is full it
is automatically synchronized.

 Explicitly, with manipulators: When certain manipulators are used on


streams, an explicit synchronization takes place. These manipulators are: flush
and endl.

 Explicitly, with member function sync(): Calling stream's member function


sync(), which takes no parameters, causes an immediate synchronization. This
function returns an int value equal to -1 if the stream has no associated buffer
or in case of failure. Otherwise (if the stream buffer was successfully
synchronized) it returns 0.

- 16 -
CHAPTER 5: FILE MANAGEMENT

Exercise
1. Write a program that accept N student record from the keyboard & store the list on a
file “D:\\ Test.txt” in a text file format
2. Write a program that reads students record from the text file “D:|\ Test.txt” and
display on the screen.
3. Do Q1 in binary format.
4. Do Q2 in binary format.
Note Student record consists of first name, last name, gender, age and Id.

Solution
Consider the file Header .h which contains the basic preprocessing include files,
structure definition and functions other than the main function.
Header. h // this file is saved at D: and it has the following structure.
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
struct studList
{
char firstName[12];
char lastName[12];
int age;
char gender;
char Id[12];
};
studList getStudent()
{
studList std;
cout<<"Enter student first name ===>";cin>>std.firstName;
cout<<"Enter student last name ===>";cin>>std.lastName;
cout<<"Enter student age ===>";cin>>std.age;
cout<<"Enter student gender ===>";cin>>std.gender;
cout<<"Enter student Id ===>";cin>>std.Id;
return std;
}
void DisplayStudent(studList std)
{
cout<<"Student first name:\t"<<std.firstName<<endl;
cout<<"Student last name :\t"<<std.lastName<<endl;
cout<<"Student age:\t\t"<<std.age<<endl;
cout<<"Student gender:\t\t"<<std.gender<<endl;
cout<<"Student Id:\t\t"<<std.Id<<endl;
}

- 17 -
CHAPTER 5: FILE MANAGEMENT

Solution 1
#include "D:\ header.h"
int main() {
studList std;
fstream outf;
outf.open("d:\\test.txt",ios::app);
if(outf.fail()) {
cout<<"unable to open the file d:\test.txt\n";
return 1;
}
clrscr();
int N;
cout<<"Enter the number of students ===> ";cin>>N;
for(int i = 0; i < N; i++) {
std = getStudent();
clrscr();
outf<<std.firstName<<" "<<std.lastName<<" "<<std.Id<<" "
<<std.gender<<" "<<std.age<<endl;
}
getch();
return 0;
}

Solution 2
#include "D:\ header.h"
int main(){
studList std;
fstream inpf;
inpf.open("d:\\test.txt",ios::in);
if(inpf.fail())
{
cout<<"unable to open the file d:\test.txt\n"; return
1;
}
clrscr();
while (!inpf.eof())
{

inpf>>std.firstName>>std.lastName>>std.Id>>std.gender>>std.age;
if(inpf.eof()) break;
DisplayStudent(std);
cout<<"=====================================\n";
getch();
}
inpf.close();
return 0;
}
Solution 3
#include "D:\ header.h"
int main()
{
studList std;
fstream outf;
outf.open("d:\\test2.txt",ios::app|ios::binary);
if(outf.fail())

- 18 -
CHAPTER 5: FILE MANAGEMENT
{
cout<<"unable to open the file d:\test.txt\n"; return
1;
}
clrscr();
int N;
cout<<"Enter the number of students ===> ";cin>>N;
for(int i = 0; i < N; i++)
{
std = getStudent();
clrscr();
outf.write((char *) &std, sizeof(std));
}
getch();
return 0;
}
Solution 4
#include "D:\ header.h"
int main() {
studList std;
fstream inpf;
inpf.open("d:\\test2.txt",ios::in|ios::binary);
if(inpf.fail())
{
cout<<"unable to open the file d:\test.txt\n";
return 1;
}
clrscr();
while (!inpf.eof()){
{
inpf.read((char*)&std, sizeof(std));

//inpf>>std.firstName>>std.lastName>>std.Id>>std.gender>>std.age;
if(inpf.eof()) break;
DisplayStudent(std);
cout<<"======================================\n";
getch();
}
inpf.close();
return 0;
}

- 19 -
CHAPTER 5: FILE MANAGEMENT

5.3. Annex

5.3.1. Prototypes of C++ file I/O methods


Void open ( const char * filename, int mode, int access = filebuf::openprot ) ;

File open modes:


ios::app Write all output to the end of the file
ios::ate Open a file for output and move to the end of the file (normally used to
append data to a file). Data can be written anywhere in the file.
ios::binary Cause the to be opened in binary mode.
ios::in Open a file for input
ios::out Open a file for output
ios::trunc Discard the file's content if it exists (this is also the default action ios::out)
ios::nocreate If the file does not exist, the open operation fails.
ios::noreplace If the file exists, the open operation fails.
istream & get ( char & ch ) ;
ostream & put ( char ch ) ;
istream & read ( unsigned char * buf, int num ) ;
ostream & write ( const unsigned char * buf, int num ) ;
int gcount ( ) ;
istream & get ( char * buf, int num, char delim = '\n' ) ;
int get ( ) ;
istream & getline ( char * buf, int num, char delim = '\n' ) ;
int eof ( );
istream & ignore ( int num = 1, int delim = EOF ) ;
int peek ( ) ;
istream & putback ( char c ) ;
ostream & flush ( ) ;
istream & seekg ( streamoff offset, seek_dir origin ) ;
ostream & seekp (streamoff offset, seek_dir origin ) ;
origin must be one of three values:
ios::beg -- Beginning of file
ios::cur -- Current location
ios::end -- End of file
streampos tellg ( ) ;
stream pos tellp ( ) ;
istream & seekg ( streampos pos ) ;
ostream * seekp (streampos pos ) ;
int rdstate ( ) ;
inr bad ( ) ;
int eof ( ) ;
int fail ( ) ;
int good ( );
void clear ( int flags = 0 ) ;

- 20 -

You might also like