Advertentie:

Auteur Topic: 25. Input/Output with files  (gelezen 396 keer)

Offline Laloeka

  • Administrator
  • Gerespecteerd lid
  • ********
  • Berichten: 1206
    • Laloeka.nl
25. Input/Output with files
« Gepost op: januari 22, 2011, 16:06:37 pm »
C++ provides the following classes to perform output and input of characters to/from files:

<ul>
<li><b>ofstream:</b> Stream class to write on files</li>
<li><b>ifstream:</b> Stream class to read from files</li>
<li><b>fstream:</b> Stream class to both read and write from/to files.</li>
</ul>

These classes are derived directly or indirectly from the classes <tt>istream</tt>, and <tt>ostream</tt>. We have already used objects whose types were these classes: <tt>cin</tt> is an object of class <tt>istream</tt> and <tt>cout</tt> is an object of class <tt>ostream</tt>. Therfore, 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 <tt>cin</tt> and <tt>cout</tt>, with the only difference that we have to associate these streams with physical files. Let's see an example:

<sourcecode language="c++">
// 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;
}
-----
[file example.txt]
Writing this to a file.
</sourcecode>

This code creates a file called <tt>example.txt</tt> and inserts a sentence into it in the same way we are used to do with <tt>cout</tt>, but using the file stream <tt>myfile</tt> instead.

But let's go step by step:

<h3>Open a file</h3>

The first operation generally performed on an object of one of these classes is to associate it to a real file. This procedure is known as to <i>open a file</i>. An open file is represented within a program by a stream object (an instantiation of one of these classes, in the previous example this was <tt>myfile</tt>) and any input or output operation performed on this stream object will be applied to the physical file associated to it.

In order to open a file with a stream object we use its member function <tt>open()</tt>:

<tt>
open (filename, mode);
</tt>

Where <tt>filename</tt> is a null-terminated character sequence of type <tt>const char *</tt> (the same type that string literals have) representing the name of the file to be opened, and <tt>mode</tt> is an optional parameter with a combination of the following flags:

<table>
<tr><td>ios::in</td><td>Open for input operations.</td></tr>
<tr><td>ios::out</td><td>Open for output operations.</td></tr>
<tr><td>ios::binary</td><td>Open in binary mode.</td></tr>
<tr><td>ios::ate</td><td>Set the initial position at the end of the file.
If this flag is not set to any value, the initial position is the beginning of the file.</td></tr>
<tr><td>ios::app</td><td>All output operations are performed at the end of the file, appending the content to the current content of the file. This flag can only be used in streams open for output-only operations.</td></tr>
<tr><td>ios::trunc</td><td>If the file opened for output operations already existed before, its previous content is deleted and replaced by the new one.</td></tr>
</table>

All these flags can be combined using the bitwise operator OR (<tt>|</tt>). For example, if we want to open the file <tt>example.bin</tt> in binary mode to add data we could do it by the following call to member function <tt>open()</tt>:

<sourcecode language="c++">
ofstream myfile;
myfile.open ("example.bin", ios::out | ios::app | ios::binary);
</sourcecode>

Each one of the <tt>open()</tt> member functions of the classes <tt>ofstream</tt>, <tt>ifstream</tt> and <tt>fstream</tt> has a default mode that is used if the file is opened without a second argument:

<table>
<tr><th>class</th><th>default mode parameter</th></tr>
<tr><td>ofstream</td><td>ios::out</td></tr>
<tr><td>ifstream</td><td>ios::in</td></tr>
<tr><td>fstream</td><td>ios::in | ios::out</td></tr>
</table>

For <tt>ifstream</tt> and <tt>ofstream</tt> classes, <tt>ios::in</tt> and <tt>ios::out</tt> are automatically and respectively assumed, even if a mode that does not include them is passed as second argument to the <tt>open()</tt> 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 <i>text files</i>, and some translations may occur due to formatting of some special characters (like newline and carriage return characters).

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 <tt>open()</tt> member function and has the exact same parameters as this member. Therefore, we could also have declared the previous <tt>myfile</tt> object and conducted the same opening operation in our previous example by writing:

<sourcecode language="c++">
ofstream myfile ("example.bin", ios::out | ios::app | ios::binary);
</sourcecode>

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 <tt>is_open()</tt> 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:

<sourcecode language="c++">
if (myfile.is_open()) { /* ok, proceed with output */ }
</sourcecode>

<h3>Closing a file</h3>
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 <tt>close()</tt>. This member function takes no parameters, and what it does is to flush the associated buffers and close the file:

<sourcecode language="c++">
myfile.close();
</sourcecode>

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 <tt>close()</tt>.

<h3>Text files</h3>
Text file streams are those where we do not include the <tt>ios::binary</tt> flag in their opening mode. These files are designed to store text and thus all values that we input or output from/to them can suffer some formatting transformations, which do not necessarily correspond to their literal binary value.

Data output operations on text files are performed in the same way we operated with <tt>cout</tt>:

<sourcecode language="c++">
// 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;
}
-----
[file example.txt]
This is a line.
This is another line.
</sourcecode>

Data input from a file can also be performed in the same way that we did with <tt>cin</tt>:

<sourcecode language="c++">
// 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.good() )
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
}

else cout << "Unable to open file";

return 0;
}
-----
This is a line.
This is another line.
</sourcecode>

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 <tt>good()</tt> that returns true in the case that the stream is ready for input/output operations. We have created a while loop that finishes when indeed <tt>myfile.good()</tt> is no longer true, which will happen either if the end of the file has been reached or if some other error occurred.

<h3>Checking state flags</h3>
In addition to <tt>good()</tt>, which checks whether the stream is ready for input/output operations, other member functions exist to check for specific states of a stream (all of them return a bool value):

<dl>
<dt>bad()</dt>
<dd>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.</dd>
<dt>fail()</dt>
<dd>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.</dd>
<dt>eof()</dt>
<dd>Returns true if a file open for reading has reached the end.</dd>
<dt>good()</dt>
<dd>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.</dd>
</dl>

In order to reset the state flags checked by any of these member functions we have just seen we can use the member function <tt>clear()</tt>, which takes no parameters.

<h3>get and put stream pointers</h3>
All i/o streams objects have, at least, one internal stream pointer:

<tt>ifstream</tt>, like <tt>istream</tt>, has a pointer known as the <i>get pointer</i> that points to the element to be read in the next input operation.

<tt>ofstream</tt>, like <tt>ostream</tt>, has a pointer known as the <i>put pointer</i> that points to the location where the next element has to be written.

Finally, <tt>fstream</tt>, inherits both, the get and the put pointers, from <tt>iostream</tt> (which is itself derived from both <tt>istream</tt> and <tt>ostream</tt>).

These internal stream pointers that point to the reading or writing locations within a stream can be manipulated using the following member functions:

<h4>tellg() and tellp()</h4>
These two member functions have no parameters and return a value of the member type <tt>pos_type</tt>, which is an integer data type representing the current position of the get stream pointer (in the case of <tt>tellg</tt>) or the put stream pointer (in the case of <tt>tellp</tt>).

<h4>seekg() and seekp()</h4>
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:

<tt>
seekg ( position );
seekp ( position );
</tt>

Using this prototype the stream pointer is changed to the absolute position <tt>position</tt> (counting from the beginning of the file). The type for this parameter is the same as the one returned by functions <tt>tellg</tt> and <tt>tellp</tt>: the member type <tt>pos_type</tt>, which is an integer value.

The other prototype for these functions is:

<tt>
seekg ( offset, direction );
seekp ( offset, direction );
</tt>

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 <tt>direction</tt>. <tt>offset</tt> is of the member type <tt>off_type</tt>, which is also an integer type. And <tt>direction</tt> is of type <tt>seekdir</tt>, which is an enumerated type (<tt>enum</tt>) that determines the point from where offset is counted from, and that can take any of the following values:

<table>
<tr><td>ios::beg</td><td>offset counted from the beginning of the stream</td></tr>
<tr><td>ios::cur</td><td>offset counted from the current position of the stream pointer</td></tr>
<tr><td>ios::end</td><td>offset counted from the end of the stream</td></tr>
</table>

The following example uses the member functions we have just seen to obtain the size of a file:

<sourcecode language="c++">
// obtaining file size
#include <iostream>
#include <fstream>
using namespace std;

int main () {
long begin,end;
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;
}
-----
size is: 40 bytes.
</sourcecode>

<h3>Binary files</h3>
In binary files, to input and output data with the extraction and insertion operators (<tt><<</tt> and <tt>>></tt>) and functions like <tt>getline</tt> 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: <tt>write</tt> and <tt>read</tt>. The first one (<tt>write</tt>) is a member function of <tt>ostream</tt> inherited by <tt>ofstream</tt>. And <tt>read</tt> is a member function of <tt>istream</tt> that is inherited by <tt>ifstream</tt>. Objects of class <tt>fstream</tt> have both members. Their prototypes are:

<tt>
write ( memory_block, size );
read ( memory_block, size );
</tt>

Where <tt>memory_block</tt> is of type "pointer to char" (<tt>char*</tt>), 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 <tt>size</tt> parameter is an integer value that specifies the number of characters to be read or written from/to the memory block.

<sourcecode language="c++">
// reading a complete binary file
#include <iostream>
#include <fstream>
using namespace std;

ifstream::pos_type size;
char * memblock;

int main () {
ifstream file ("example.bin", 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;
}
-----
the complete file content is in memory
</sourcecode>

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 <tt>ios::ate</tt> flag, which means that the get pointer will be positioned at the end of the file. This way, when we call to member <tt>tellg()</tt>, we will directly obtain the size of the file. Notice the type we have used to declare variable <tt>size</tt>:

<sourcecode language="c++">
ifstream::pos_type size;
</sourcecode>

<tt>ifstream::pos_type</tt> is a specific type used for buffer and file positioning and is the type returned by <tt>file.tellg()</tt>. 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 <tt>int</tt>:

<sourcecode language="c++">
int size;
size = (int) file.tellg();
</sourcecode>

Once we have obtained the size of the file, we request the allocation of a memory block large enough to hold the entire file:

<sourcecode language="c++">
memblock = new char[size];
</sourcecode>

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:

<sourcecode language="c++">
file.seekg (0, ios::beg);
file.read (memblock, size);
file.close();
</sourcecode>

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.

<h3>Buffers and Synchronization</h3>

When we operate with file streams, these are associated to an internal buffer of type <tt>streambuf</tt>. This buffer is a memory block that acts as an intermediary between the stream and the physical file. For example, with an <tt>ofstream</tt>, each time the member function <tt>put</tt> (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 <i>synchronization</i> and takes place under any of the following circumstances:

<ul>
<li><b>When the file is closed:</b> 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.</li>
<li><b>When the buffer is full:</b> Buffers have a certain size. When the buffer is full it is automatically synchronized.</li>
<li><b>Explicitly, with manipulators:</b> When certain manipulators are used on streams, an explicit synchronization takes place. These manipulators are: <tt>flush</tt> and <tt>endl</tt>.</li>
<li><b>Explicitly, with member function sync():</b> Calling stream's member function <tt>sync()</tt>, which takes no parameters, causes an immediate synchronization. This function returns an <tt>int</tt> value equal to <tt>-1</tt> if the stream has no associated buffer or in case of failure. Otherwise (if the stream buffer was successfully synchronized) it returns <tt>0</tt>.</li>
</ul>

Offline jeetee

  • Zeer actief lid
  • *****
  • Berichten: 255
Re:25. Input/Output with files
« Reactie #1 Gepost op: februari 02, 2011, 18:17:37 pm »
[placeholder]
2 + 2 = 5, within a large range of 2

Offline Divendo

  • Administrator
  • Zeer gerespecteerd lid
  • ********
  • Berichten: 2309
  • Voer in op google maps: 37.971458,23.726706
    • Divendo Webs
Re:25. Input/Output with files
« Reactie #2 Gepost op: juni 27, 2011, 13:24:32 pm »
Ga je deze nog doen? Want anders moet je even je placeholder weghalen ;)
Admin - http://divendo-webs.com
[C++][PHP][JavaScript][HTML][XML][CSS][SQL][Bash][GML][Nederlands][Engels][Latijn]

"Leren is proberen."

Offline jeetee

  • Zeer actief lid
  • *****
  • Berichten: 255
Re:25. Input/Output with files
« Reactie #3 Gepost op: juni 27, 2011, 14:27:59 pm »
oepsie.. die ligt hier al een tijdje voor 2/3de af te zijn.. uit het oog verloren; kzal het volgende week proberen afwerken zie.
2 + 2 = 5, within a large range of 2

Online jonas

  • Ervaren lid
  • ******
  • Berichten: 726
Re: 25. Input/Output with files
« Reactie #4 Gepost op: november 26, 2011, 20:56:07 pm »
Ben je er nog mee bezig? Anders wil ik er wel aan beginnen. :)
« Laatst bewerkt op: november 27, 2011, 14:45:52 pm door jonas »

Advertentie: