- ofstream: Stream class to write on files
- ifstream: Stream class to read from files
- fstream: Stream class to both read and write from/to files.
Open:
mode:
Close:
- #include <iostream>
-
#include <fstream>
-
#include <string>
-
using namespace std;
-
-
int
-
main(void)
-
{
-
ofstream myfile("example.txt");
-
if (myfile.is_open()) {
-
myfile << "This is a line." << endl;
-
myfile << "This is another line." << endl;
-
myfile.close();
-
} else {
-
cout << "Unable to open file" << endl;
-
}
-
-
string line;
-
ifstream myfile2("example.txt");
-
if (myfile2.is_open()) {
-
while (myfile2.good()) {
-
getline(myfile2, line);
-
cout << line << endl;
-
}
-
myfile2.close();
-
} else {
-
cout << "Unable to open file: 2" << endl;
-
}
-
-
return (0);
-
}
state flag:
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.
tellg() 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() seekp()
These functions allow us to change the position of the get and put
stream pointers. Both functions are overloaded with two different
prototypes.
- seekg ( position );
-
seekp ( position );
- seekg ( offset, direction );
-
seekp ( offset, direction );
direction:
Binary file:
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.
- write ( memory_block, size );
-
read ( memory_block, size );
Buffer 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.
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.
阅读(1910) | 评论(0) | 转发(0) |