#include <string>
#include <iostream>
int main() {
std::string str;
char magic[9];
std::cin.read((char *)magic, sizeof(magic));
std::cin.seekg(0, std::ios::beg);
while (std::cin >> str) {
std::cout << str << std::endl;
}
}
my code contains implementation of seekg(0) fucntion on std::cin
it is not behaving as expected on some of the files
when run as
./a.out < filename
those files that it is not behaving as expected have property that they have number of characters(including endline characters and other white spaces) less than 9(9 is the number of characters we read from cin before seekg)
if the file contains more than 9 characters it is behaving as expected for example:
123456789
will give output as
123456789
while file containing less than 9 characters will not give output
for example:
1234
will give no output
With a file of less than nine characters, you have already attempted to read past the end with your initial
read. That means theeof(end of file) andfailflags have been set for the stream and, whileseekgmay reseteof, it does not resetfail(a).You can check that by inserting:
immediately before and after the
seekg. For file sizes of 8, 9, and 10 respectively, you get:You can see the first failure results in no output because the
failbit is still set. The second and third have output because it was never set (the output is the characters shown plus one newline).To repair this, you can clear the
failbit simply by inserting the following before yourseekg:Then running that code on the eight-character file gives:
showing that the
clearhas indeed cleared thefailbit.You might also want to keep in mind that it's not a requirement for a stream to be seekable, especially if it's just coming in via standard input. You may find for certain sized files that you cannot seek back an arbitrary amount if you've read through a large chunk of the stream.
(a) For the language lawyers amongst us,
Unformatted input functions(C++11 27.7.2.3/41,C++14 27.7.2.3/41andC++17 30.7.4.3/41) all have essentially the same text on howseekgworks (my emphasis):