fstream
Class fstream
template<
class CharT,
class Traits = std::char_traits<CharT>
>
class basic_fstream : public std::basic_iostream<CharT, Traits>
The class template basic_fstream implements high-level input/output operations on file based streams. It interfaces a file-based streambuffer (std:: basic_filebuf) with the high-level interface of (std::basic_iostream).
A typical implementation of std::basic_fstream holds only one non-derived data member: an instance of std::basic_filebuf<CharT, Traits>.
| Type |
Definition |
| std::fstream |
std::basic_fstream |
| std::wfstream |
std::basic_fstream<wchar_t> |
| Member type |
Definition |
| char_type |
CharT |
| traits_type |
Traits; the program is ill-formed if Traits::char_type is not CharT. |
| int_type |
Traits::int_type |
| pos_type |
Traits::pos_type |
| off_type |
Traits::off_type |
| native_handle_type(c++26) |
implementation-defined type that is TriviallyCopyable and semiregular |
| function |
definition |
| (constructor) |
constructs the file stream |
| (destructor) |
destructs the basic_fstream and the associated buffer, closes the file |
| operator= (c++11) |
moves the file stream |
| swap (c++11) |
swaps two file streams |
| rdbuf |
returns the underlying raw file device object |
| native_handle (c++26) |
returns the underlying implementation-defined handle |
| is_open |
checks if the stream has an associated file |
| open |
opens a file and associates it with the stream |
| close |
closes the associated file |
| function |
definition |
| std::swap (c++11) |
specializes the std::swap algorithm |
-
inherited from std::basic_istream
| function |
definition |
| operator>> |
extracts formatted data |
| get |
extracts characters |
| peek |
reads the next character without extracting it |
| unget |
unextracts a character |
| putback |
puts a character into input stream |
| getline |
extracts characters until the given character is found |
| ignore |
extracts and discards characters until the given character is found |
| read |
extracts blocks of characters |
| readsome |
extracts already available blocks of characters |
| gcount |
returns number of characters extracted by last unformatted input operation |
| tellg |
returns the input position indicator |
| seekg |
sets the input position indicator |
| sync |
synchronizes with the underlying storage device |
| sentry |
implements basic logic preparation of the stream for input operations |
-
inherited from std::basic_ostream
-
Member functions
| function |
definition |
| operator<< |
inserts formatted data |
| put |
inserts a character |
| write |
inserts blocks of characters |
| tellp |
returns the output position indicator |
| seekp |
sets the output position indicator |
| flush |
synchronizes with the underlying storage device |
| sentry |
implements basic logic for preparation of the stream for output operations |
-
inherited from std::basic_ios
| Member type |
Definition |
| char_type |
CharT |
| traits_type |
Traits |
| int_type |
Traits::int_type |
| pos_type |
Traits::pos_type |
| off_type |
Traits::off_type |
| function |
Definition |
| good |
checks if no error has occurred i.e. I/O operations are available |
| eof |
checks if end-off-file has been reached |
| fail |
checks if an error has occurred |
| bad |
checks if a non-recoverable error has occurred |
| operator! |
checks if an error has occurred (synonym of fail()) |
| operator bool |
checks if no error has occurred (synonym of !fail()) |
| rdstate |
returns state flags |
| setstate |
sets state flags |
| clear |
modifies state flags |
| copyfmt |
copies formatting information |
| fill |
manages the fill character |
| exceptions |
manages exception mask |
| imbue |
sets the locale |
| rdbuf |
manages associated stream buffer |
| narrow |
narrows characters |
| widen |
widens characters |
-
Inherited from std::ios_base
| function |
Definition |
| flags |
manages format flags |
| setf |
sets specific format flag |
| unsetf |
clears specific format flag |
| precision |
manages decimal precision of floating point operations |
| width |
manages field width |
| imbue |
sets locale |
| getloc |
returns current locale |
| xalloc |
returns a program-wide unique integer that is safe to use as index to pword() and iword() |
| iword |
resizes the private storage if necessary and access to the long element at the given index |
| pword |
resizes the private storage if necessary and access to the void* element at the given index |
| register_callback |
registers event callback function |
| sync_with_stdio |
sets whether C++ and C I/O libraries are interoperable |
| failure |
stream exception |
| Init |
initializes standard stream objects |
-
Example
fstream = read + write file stream
#include <fstream>
#include <iostream>
using namespace std;
- Open file (read + write)
fstream fs("test.txt", ios::in | ios::out);
- Open mode (important)
| mode |
remark |
| ios::in |
read |
| ios::out |
write |
| ios::app |
append write |
| ios::trunc |
open and clean the file (default) |
| ios::binary |
binary mode |
| ios::ate |
jump to the end of file when open it |
fstream fs("test.txt", ios::in | ios::out);
fstream fs("test.txt", ios::in | ios::out | ios::trunc);
fstream fs("test.txt", ios::out | ios::app);
fstream fs("test.bin", ios::out | ios::in | ios::binary);
- Using seekg/seekp to mode read/write position
| Action |
remark |
| Move read pointer |
fs.seekg(offset, whence) |
| Move write pointer |
fs.seekp(offset, whence) |
| Move all |
fs.seekg(pos); fs.seekp(pos); |
fs.seekg(0);
fs.seekg(0);
whence:
- ios::beg // start
- ios::cur // current pos
- ios::end // end of file
- Example
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main() {
fstream fs("test.txt", ios::in | ios::out | ios::trunc);
if (!fs) {
cout << "打开文件失败" << endl;
return 0;
}
fs << "Hello World\n";
string line;
while(getline(fs, line)) {
cout << line << endl;
}
fs.close();
}
#include <fstream>
#include <iostream>
#include <string>
int main() {
std::string filename{"test.bin"};
std::fstream s{filename, s.binary | s.trunc | s.in | s.out};
if (!s.is_open()) {
std::cout << "failed to open " << filename << "\n";
} else {
double d{3.14};
s.write(reinterpret_cast<char*>((&d), sizeof d));
s << 123 << "abc";
s.seekp(0);
d = 2.781828;
s.read(reinterpret_cast<char*>((&d), sizeof d));
int n;
std::string str;
if (s >> n >> str) {
std::cout << "read back from file: " << d << " " << n << str << "\n";
}
}
}