.\" ident @(#)basic_fstream.3 .\" Standard Template Library .\" $$RW_INSERT_HEADER "slyrs.man" .TH basic_fstream 3C++ "02 Apr 1998" "Rogue Wave Software" "-" .ce2 Standard C++ Library Copyright 1998, Rogue Wave Software, Inc. .SH NAME \f2basic_fstream\fP, \f2fstream\fP, \f2wfstream\fP \ - Supports reading and writing of named files or devices associated with a file descriptor. .SH SYNOPSIS .br #include .br template > .br class basic_fstream .br : public basic_iostream .SH DESCRIPTION The template class basic_fstream supports reading and writing to named files or other devices associated with a file descriptor. It uses a \f2basic_filebuf\fP object to control the associated sequences. It inherits from basic_iostream and can therefore use all the formatted and unformatted input and output functions. .SH INTERFACE .br template > .br class basic_fstream .br : public basic_iostream { .br .br public: .br .RE .RS 1 typedef basic_ios ios_type; .RE .RS 0 .RE .RS 1 typedef charT char_type; .br typedef traits traits_type; .br typedef typename traits::int_type int_type; .br typedef typename traits::pos_type pos_type; .br typedef typename traits::off_type off_type; .RE .RS 0 .RE .RS 1 basic_fstream(); .br explicit basic_fstream(const char *s, ios_base::openmode .RE .RS 24 mode = ios_base::in | .br ios_base::out, .br long protection = 0666); .RE .RS 0 .RE .RS 1 explicit basic_fstream(int fd); .br basic_fstream(int fd, char_type *buf, int len); .RE .RS 0 .RE .RS 1 virtual ~basic_fstream(); .RE .RS 0 .RE .RS 1 basic_filebuf *rdbuf() const; .br bool is_open(); .br void open(const char *s, ios_base::openmode mode = .RE .RS 11 ios_base::in | ios_base::out, .br long protection = 0666); .RE .RS 0 .RE .RS 1 void close(); .RE .RS 0 .br }; .SH TYPES .br char_type .RE .RS 3 The type \f2char_type\fP is a synonym for the template parameter \f2charT\fP. .RE .br fstream .RE .RS 3 The type \f2fstream\fP is an instantiation of class \f2basic_fstream\fP on type \f2char\fP: \f2typedef basic_fstream fstream;\fP .RE .br int_type .RE .RS 3 The type\f2 int_type\fP is a synonym of type \f2traits::in_type\fP. .RE .br ios_type .RE .RS 3 The type \f2ios_type\fP is an instantiation of class \f2basic_ios\fP on type \f2charT\fP. .RE .br off_type .RE .RS 3 The type\f2 off_type\fP is a synonym of type \f2traits::off_type\fP. .RE .br pos_type .RE .RS 3 The type \f2pos_type\fP is a synonym of type \f2traits::pos_type\fP. .RE .br traits_type .RE .RS 3 The type \f2traits_type\fP is a synonym for the template parameter \f2traits\fP. .RE .br wfstream .RE .RS 3 The type \f2wfstream\fP is an instantiation of class \f2basic_fstream\fP on type \f2wchar_t:\fP \f2typedef basic_fstream wfstream;\fP .RE .SH CONSTRUCTORS .br basic_fstream(); .RE .RS 3 Constructs an object of class basic_fstream, initializing the base class basic_iostream with the associated file buffer, which is initialized by calling the \f2basic_filebuf\fP constructor \f2basic_filebuf()\fP. After construction, a file can be attached to the basic_fstream_object by using the \f2open()\fP member function. .RE .br basic_fstream(const char* s, .RE .RS 13 ios_base::openmode mode= .br ios_base::in | iosw_base::out, .br long protection= 0666); .RE .RS 3 Constructs an object of class basic_fstream, initializing the base class basic_iostream with the associated file buffer, which is initialized by calling the \f2basic_filebuf\fP constructor \f2basic_filebuf()\fP. The constructor then calls the open function \f2open(s,mode,protection)\fP in order to attach the file, whose name is pointed to by \f2s\fP, to the basic_fstream object. The third argument, \f2protection\fP, is used as the file permission. It does not appear in the Standard C++ description and is included as an extension. 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. .RE .RE .RS 0 explicit basic_fstream(int fd); .RE .RS 3 Constructs an object of class basic_fstream, initializing the base class basic_iostream with the associated file buffer, which is initialized by calling the \f2basic_filebuf\fP constructor \f2basic_filebuf()\fP. The constructor then calls the \f2basic_filebuf\fP open function \f2open(fd)\fP in order to attach the file descriptor \f2fd\fP to the basic_fstream object. This constructor is not described in the C++ standard, and is included as an extension in order to manipulate pipes, sockets, or other UNIX devices that can be accessed through file descriptors. If the function fails, it sets \f2ios_base::failbit.\fP .RE .br basic_fstream(int fd, char_type* buf,int len); .RE .RS 3 Constructs an object of class basic_fstream, initializing the base class basic_iostream with the associated file buffer, which is initialized by calling the \f2basic_filebuf\fP constructor \f2basic_filebuf()\fP. The constructor then calls the \f2basic_filebuf\fP open function \f2open(fd)\fP in order to attach the file descriptor \f2fd\fP to the basic_fstream object. The underlying buffer is then replaced by calling the \f2basic_filebuf\fP member function, \f2setbuf()\fP, with parameters \f2buf\fP and \f2len\fP. This constructor is not described in the C++ standard, and is included as an extension in order to manipulate pipes, sockets, or other UNIX devices that can be accessed through file descriptors. It also maintains compatibility with the old iostreams library. If the function fails, it sets \f2ios_base::failbit.\fP .RE .SH DESTRUCTORS .br virtual ~basic_fstream(); .RE .RS 3 Destroys an object of class \f2basic_fstream\fP. .RE .SH MEMBER FUNCTIONS .br void .br close(); .RE .RS 3 Calls the associated \f2basic_filebuf\fP function \f2close()\fP and if this function fails, it calls the \f2basic_ios\fP member function \f2setstate(failbit)\fP. .RE .br bool .br is_open(); .RE .RS 3 Calls the associated \f2basic_filebuf\fP function\f2 is_open()\fP and return its result. .RE .br void .br open(const char* s,ios_base::openmode = .RE .RS 4 ios_base::out | ios_base::in, .br long protection = 0666); .RE .RS 3 Calls the associated \f2basic_filebuf\fP function \f2open(s,mode,protection)\fP and, if this function fails at opening the file, calls the \f2basic_ios\fP member function \f2setstate(failbit)\fP. The third argument \f2protection\fP is used as the file permissions. It does not appear in the Standard C++ description and is included as an extension. 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. .RE .RE .RS 0 basic_filebuf* .br rdbuf() const; .RE .RS 3 Returns a pointer to the \f2basic_filebuf\fP associated with the stream. .RE .SH EXAMPLE .br // .br // stdlib/examples/manual/fstream.cpp .br // .br #include .br #include .br void main ( ) .br { .RE .RS 3 using namespace std; .RE .RS 0 .RE .RS 4 // create a bi-directional fstream object .RE .RS 3 fstream inout("fstream.out"); .RE .RS 0 .RE .RS 4 // output characters .RE .RS 3 inout << "Das ist die rede von einem man" << endl; .br inout << "C'est l'histoire d'un home" << endl; .br inout << "This is the story of a man" << endl; .RE .RS 0 .RE .RS 3 char p[100]; .RE .RS 0 .RE .RS 4 // seek back to the beginning of the file .RE .RS 3 inout.seekg(0); .RE .RS 0 .RE .RS 4 // extract the first line .RE .RS 3 inout.getline(p,100); .RE .RS 0 .RE .RS 4 // output the first line to stdout .RE .RS 3 cout << endl << "Deutch :" << endl; .br cout << p; .RE .RS 0 .RE .RS 3 fstream::pos_type pos = inout.tellg(); .RE .RS 0 .RE .RS 4 // extract the second line .RE .RS 3 inout.getline(p,100); .RE .RS 0 .RE .RS 4 // output the second line to stdout .RE .RS 3 cout << endl << "Francais :" << endl; .br cout << p; .RE .RS 0 .RE .RS 4 // extract the third line .RE .RS 3 inout.getline(p,100); .RE .RS 0 .RE .RS 4 // output the third line to stdout .RE .RS 3 cout << endl << "English :" << endl; .br cout << p; .RE .RS 0 .RE .RS 4 // move the put sequence before the second line .RE .RS 3 inout.seekp(pos); .RE .RS 0 .RE .RS 4 // replace the second line .RE .RS 3 inout << "This is the story of a man" << endl; .RE .RS 0 .RE .RS 4 // replace the third line .RE .RS 3 inout << "C'est l'histoire d'un home"; .RE .RS 0 .RE .RS 4 // seek to the beginning of the file .RE .RS 3 inout.seekg(0); .RE .RS 0 .RE .RS 4 // output the all content of the fstream .br // object to stdout .RE .RS 3 cout << endl << endl << inout.rdbuf(); .RE .RS 0 } .SH SEE ALSO char_traits(3C++), ios_base(3C++), basic_ios(3C++), basic_filebuf(3C++), basic_ifstream(3C++), basic_ofstream(3C++) Working Paper for Draft Proposed International Standard for Information Systems--Programming Language C++, Section 27.8.1.11 .SH STANDARDS CONFORMANCE ANSI X3J16/ISO WG21 Joint C++ Committee