I am trying to read a redirected file using cin.get(). This is my attemp at using seekg, but it's not working correctly.
int temp;
while(cin.get(temp))
{
//code here
}
cin.seekg(0,ios::beg);
if(cin.fai())
{
cout << "failed";// it fails
}
I am trying to read a redirected file using cin.get(). This is my attemp at using seekg, but it's not working correctly.
int temp;
while(cin.get(temp))
{
//code here
}
cin.seekg(0,ios::beg);
if(cin.fai())
{
cout << "failed";// it fails
}
In general, redirected files do not support seek
operations, because the input goes through stdin
(or it's equivalent), which doesn't support "move around" operations. This is because although there may be a small internal buffer, the expectation is that "once something has been read, it's no longer available on the input".
If you want to be able to go back and forth, either open the file directly, or create your own buffer (or find a way to read the input just once!)