Streams and File Input and Output in C++ Assignment Help

Assignment Help: >> C++ Programming >> Streams and File Input and Output in C++

Stream

C++ renders the accompanying classes to execute output and input of characters to/from files:

ñ  ofstream: Stream class to compose on  the files

ñ  ifstream: Stream class to read from the files

ñ  fstream: Stream class to the both read and compose from or to the  files.

These classes are derived at once or indirectly from classes  ostream and istream. Computer programmer have so soon employed objects whose forms were these classes: cin is the object of class istream and cout is the object of class ostream. Consequently, computer programmer have already been employing classes that are related to the file streams.

Open the file

The first operation by and large executed on the object of one of these classes is to related it to the real file. This procedure is referred as to open the file. An open file is constituted within the program by the stream object (the instantiation of one of these classes, in the former illustration this was myfile) and any input or output operation executed on this stream object will be employed to the physical file linked to it.

In order to open the file with the stream object computer programmer employ its member function open():

open (filename, mode);

For ifstream and ofstream classes, ios::in and ios::out are mechanically and respectively assumed, even if the mode that does not comprise them is passed as second argument to the open() member function.

The default value is only employed if the function is referred as without determining any value for the mode parametric quantity. If the function is referred as with whatever value in that parametric quantity the default mode is overridden, not aggregated.

File streams opened up in binary mode execute output  and input operations apart from others format conditions. Non-binary files are referred as text files, and some translations might take place due to formatting of some particular characters (like carriage return  and newline characters).

Since the first task that is executed on the file stream object is by and large to open the file, these three classes comprise the constructor that mechanically calls the open() member function and has the exact similar parametric quantity as this member.

Input/Output with files

C++ renders the accompanying classes to execute output and input of characters to/from files:

ñ  ofstream: Stream class to compose on the  files

ñ  ifstream: Stream class to read from  the files

ñ  fstream: Stream class to both read and compose from or to files.

These classes are inferred at once or circuitously from  classes ostream and istream. Computer programmer have already employed objects whose forms were these classes: cin is the object of class istream and cout is the object of class ostream. Thus, computer programmer have so soon been employing classes that are associated to the file streams. And in conception, computer programmer could employ the file streams the similar mode computer programmer are already employed to employ cin and cout, with the only deviation that computer programmer have to related these streams with physical files.

Using Streams

Open the file

The initiative operation by and large executed on the object of one of these classes is to related it to the real file. This operation is referred as to open the file. An open file is constituted among the program by the stream object (the instantiation of one of these classes, in the former illustration this was myfile) and any input or output operation executed on this stream object will be employed to the physical file linked to it.

 

In order to open the file with the stream object computer programmer employ its member function open():

 

open (filename, mode);

Where filename is the null-terminated character sequence of type const char * (the similar type that string literals have) constituting the name of the file to be opened and mode is the nonmandatory parametric quantity with the combining of the accompanying flags:

 

ñ  ios::in Open for input operations.

ñ  ios::out         Open for output operations.

ñ  ios::binary     Open in binary mode.

ñ  ios::ate         Set the starting position at the finish of the file.

ñ  If the flag is not set to any of the value, the starting position is  starting of  file.

ñ  ios::app        All output operations are executed at the terminal of the file, supplementing the content to the current content of the file. This flag could only be employed in streams open for output-only operations.

ñ  ios::trunc      If  file opened for output operations so soon subsisted ahead, its former message is erased and substituted by the new one.

 

Each one of the open() member functions of the classes ofstream, ifstream and fstream has the default mode that is employed if the file is opened without the second argument:

 

class   default mode parametric quantity

ofstream       ios::out

ifstream        ios::in

fstream         ios::in | ios::out

 

For ifstream and ofstream classes, ios::in and ios::out are mechanically and respectively assumed, even if  mode that does not comprise them is passed as second argument to  open() member function.

 

The default value is only employed if the function is referred as without determining any value for the mode parametric quantity. If the function is referred as with any value in that parametric quantity the default mode is overridden, not combined.

 

File streams opened in binary mode execute input and output operations independently of any format considerations. Non-binary files are referred as text files, and some translations might take place due to formatting of some particular characters (like newline and carriage return characters).

As the first task that is executed on the file stream object is by and large to open the file, these three classes comprise the constructor that mechanically calls the open() member function and has the accurate similar parametric quantity as this member. Therefore, computer programmer could also have declared the former myfile object and carried on the similar curtain raising operation in the former illustration by composing:

 

ofstream myfile ("illustration.bin", ios::out | ios::app | ios::binary);

 

Put together stream opening and object building and in the individual statement. Both classes to open the file are equivalent and valid.

 

To assure if the file stream was productive opening the file, computer programmer could do it by calling to member is_open() with no arguments. This member function returns the bool value of true in the event that surely the stream object is linked with the open file or false differently:

 if (myfile.is_open()) { /* ok, proceed with output */ }

 

Closing the file

When computer programmer are completed with the  output and input operations on the file computer programmer will close it. Thus that its resources turn usable again. In order to do that computer programmer have to call the stream's member function close(). This member function takes no parametric quantity and what it executes is to flush  linked buffers and close the file:

myfile.close();

 

As soon as this member function is called, the stream object could be employed to open some  other file, and the file is usable again to be opened up by other procedures.

In event that the object is destroyed although still linked with the open file, the destructor mechanically calls the member function close().

 

 

Checking state flags

In add-on to good(), which checks whether the stream is ready for input/output operations, other member functions subsist to check for particular states of the stream (all of them return the bool value):

 

bad()

ñ  Returns true if the interpreting or composing operation fails. For illustration in the event that computer programmer try to compose to the file that is not open for composing or if the instrument where computer programmer try to compose has no space left.

fail()

ñ  Returns true in the similar cases as bad(), but also in the event that the format error happens, like when the alphabetical character is elicited when computer programmer are assaying to read the integer number.
eof()

ñ  Returns true if the file open for interpreting has arrived at the terminal.

good()

ñ  It is the very general state flag: it returns false in the similar cases in which calling any of  former functions would return true.

 

 

In order to reset the state flags checked by any of these member functions computer programmer have just seen computer programmer could employ the member function clear(), which takes no parametric quantity.

 

ñ  get and put stream pointers: All the i/o streams objects have at least, a internal stream pointer.

 

ñ  ifstream, such as  istream, has  pointer referred as the get pointer that points to the component to be read in  following input operation.

 

ñ  ofstream, like ostream, has the pointer referred as the put pointer that points to the localization where the following component has to be composed.

 

ñ  At last, fstream, inherits both, the get and the put pointers, from iostream that is itself inferred from both  the istream and  the ostream.

 

These internal stream pointers that point to the interpreting or composing locations within the stream could be manipulated employing the accompanying member functions:

tellg() and tellp()

These two member functions have no parametric quantity and return the value of the member type pos_type, which is the integer data type constituting the latest perspective of the  put stream pointer (in  event of tellp) and  get stream pointer (in event of tellg).

seekg() and seekp()

These functions permit us to alter the position of the put and get stream pointers. Both functions are overloaded with 2 various prototypes. The first one is:

seekg ( position );
seekp ( position );
Employing this prototype  stream pointer is altered to  downright position position (calculating from the starting of the file). The sort for this parametric quantity is the similar as the one returned by functions tellp and tellg: the member type pos_type, which is the integer value.

The another type for these functions is:
seekg ( offset, direction );
seekp ( offset, direction );

Employing  this prototype, the position of the get or put pointer is set to the offset value relative to some particular point determined by the parametric quantity direction. offset is of the member type off_type, which is also the integer type. And direction is of type seekdir, which is the enumerated type (enum) that finds out the point from where offset is counted from and that could assume any of the accompanying values:

ios::beg

offset counted from the starting of the stream

ios::cur

offset counted from  current position of  stream pointer

ios::terminal

offset counted from the terminal of the stream

 

 

Binary files

In binary files, to output  and input data with the insertion  and extraction operators (<< and >>) and functions like getline is not effective, as computer programmer do not demand to format any data, and data might not employ the breakup codes employed by text files to break components (like newline, space etc...).

 

File streams comprise two member functions in distinction from others planned to input and output binary data consecutively:read and write. The first one (compose) is the member function of ostream transmitted by ofstream.

 

Buffers and Synchronization

When computer programmer operate with file streams, these are linked to the inner buffer of type streambuf. This buffer is the memory block that behaves as the intermediary among the stream and the physical file. For illustration, with the ofstream, each time the member function put which composes the single character is referred as, the character is not mentioned at once to  physical file with which  stream is linked. As an alternative to that, the character is introduced in the intermediate buffer of the  stream.
When the buffer is flushed, all the data comprised in it is composed to the physical medium (if it is the output stream) or just released if it is the input stream. This procedure is referred as synchronization and occurs under any of the accompanying considerations:

ñ  When the file is closed down: Before closing the file all buffers that have not however been evened out are synched and all unfinished data is mentioned or interpret to  physical medium.

ñ  When buffer is full: Buffers have the definite size. When the buffer is full it is mechanically synched .

ñ  In an explicit manner, with manipulators: When definite manipulators are employed on streams, the expressed synchronization takes place. These operators are: endl and  flush.

ñ  In an explicit manner, with member function sync(): Calling stream's member function sync(), which assumes no parametric quantity, induces straightaway synchronization. This function returns the int value equal to -1 if the stream has no linked buffer or in event of failure. In other respects, if the stream buffer was  in a successful manner synched it returns 0.

Students can get solutions for Streams and File Input and Output in C++  online. ExpertsMinds interactive academic session will make learning  Streams and File Input and Output in C++ easy. Get answers online to all the questions, assignments, homework on Streams and File Input and Output in C++ , under the expert guidance of our tutors. Expertsmind.com offers Streams and File Input and Output in C++ online tutoring service,  Streams and File Input and Output in C++ homework help and Streams and File Input and Output in C++ anytime from anywhere 24x7

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd