The class template basic_fstream supports reading and writing to named files or other devices associated with a file descriptor. It uses a basic_filebuf object to control the associated sequences. It inherits from basic_iostream and can therefore use all the formatted and unformatted input and output functions.

Constructs an object of class basic_fstream, initializing the base class basic_iostream with the associated file buffer. This buffer is initialized by calling the basic_filebuf constructor:

basic_filebuf<char_type, traits_type>(rdbuf())

The constructor then calls:

rdbuf()->open(s, mode, protection)

in order to attach the file, whose name is pointed to by s, to the basic_fstream object.

The third argument, protection, holds file permissions. It determines the file read/write/execute permissions under UNIX, but is more limited elsewhere.

NOTE -- The protection argument is not part of the C++ Standard, but is included here as a convenience extension. See Appendix B for a complete list of extensions of this implementation.

basic_fstream(int fd, char_type *buf, streamsize n);

Constructs an object of class basic_fstream, initializing the base class basic_iostream with the associated file buffer. This buffer is initialized by calling the basic_filebuf constructor:

basic_filebuf<char_type, traits_type>(rdbuf ())

The constructor then calls:

rdbuf()->open (fd, buf, n)

in order to attach the file descriptor fd to the basic_fstream object. If the function fails, it sets ios_base::failbit.

NOTE -- This function is not part of the C++ Standard, but is included here as an extension in order to manipulate pipes, sockets, or other UNIX devices that can be accessed through file descriptors. See Appendix B for a complete list of extensions of this implementation.

basic_fstream(FILE *fp, char_type* buf, streamsize n);

Constructs an object of class basic_fstream, initializing the base class basic_iostream with the associated file buffer. This buffer is initialized by calling the basic_filebuf constructor:

basic_filebuf<char_type, traits_type>(rdbuf ())

The constructor then calls:

rdbuf()->open (fp, buf, n)

in order to attach the file pointer fp to the basic_fstream object. If the function fails, it sets ios_base::failbit.

NOTE -- This function is not part of the C++ Standard, but is provided here as a convenience extension. See Appendix B for a complete list of extensions of this implementation.

Calls rdbuf()->open(s, mode). If this function fails to open the file, it calls the basic_ios member function setstate(failbit).

void
open(const char* s,
ios_base::openmode, long protection);

Calls rdbuf()->open (s, mode, protection). If this function fails to open the file, it calls setstate(failbit).

The third argument, protection, holds file permissions. It determines the file read/write/execute permissions under UNIX. It is more limited under DOS, since files are always readable and do not have special execute permission.

NOTE -- The protection argument does not appear in the C++ Standard C++, but is included here as a convenience extension. See Appendix B for a complete list of extensions of this implementation.

//
// fstream.cpp
//
#include <fstream> // for fstream
#include <iostream> // for cout, endl
#include <string> // for string
int main ( )
{
// create a bi-directional fstream object
std::fstream inout ("test_fstream.out",
std::ios::in | std::ios::out |
std::ios::trunc);
// write out three lines to the file
inout << "Dieses ist die Geschichte eines Mannes.\n"
<< "C'est l'histoire d'un homme.\n"
<< "This is the story of a man." << std::endl;
std::string line;
// seek back to the beginning of the file
inout.seekg (0);
// extract the first line
std::getline (inout, line);
// output the first line to standard output
std::cout << "\nDeutsch:\n" << line << std::endl;
// obtain current position in file
std::fstream::pos_type pos = inout.tellg ();
// extract the second line
std::getline (inout, line);
// output the second line to standard output
std::cout << "Français:\n" << line << std::endl;
// extract the third line
std::getline (inout, line);
// output the third line to standard output
std::cout << "English:\n" << line << std::endl;
// move the put sequence before the second line
inout.seekp (pos);
// replace the second and third lines
inout << "This is the story of a man.\n"
<< "C'est l'histoire d'un homme.";
// seek to the beginning of the file
inout.seekg (0);
// output the all content of the fstream object to stdout
std::cout << "\n" << inout.rdbuf () << std::endl;
return 0;
}
Program Output:

Deutsch:
Dieses ist die Geschichte eines Mannes.
Francais:
C'est l'histoire d'un homme.
English:
This is the story of a man.
Dieses ist die Geschichte eines Mannes.
This is the story of a man.
C'est l'histoire d'un homme.