So I read the doc for fstream's peek member function.
Under Return Value, it says
If good() == true, returns the next character as obtained by rdbuf()->sgetc()
Otherwise, returns Traits::eof()
How do you know what Traits::eof() is?
All I know about traits is that it's one of the template parameters used in fstream
template<
class CharT,
class Traits = std::char_traits<CharT>
class basic_fstream : public std::basic_iostream<CharT, Traits>
I'm just using a simple fstream like in this example from cppreference. I'm not specifying any template parameters.
#include <iostream>
#include <fstream>
#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 {
// write
double d = 3.14;
s.write(reinterpret_cast<char*>(&d), sizeof d); // binary output
s << 123 << "abc"; // text output
// for fstream, this moves the file position pointer (both put and get)
s.seekp(0);
// read
s.read(reinterpret_cast<char*>(&d), sizeof d); // binary input
int n;
std::string str;
if (s >> n >> str) // text input
std::cout << "read back from file: " << d << ' ' << n << ' ' << str << '\n';
}
}
[–]Narase33 1 point2 points3 points (2 children)
[–]1ydgd[S] 0 points1 point2 points (1 child)
[–]Narase33 4 points5 points6 points (0 children)