0

Here is a minimalist code :

#include <string>
#include <sstream>
#include <iostream>

int main ()
    {
    std::string str = "spam" ;
    std::istringstream isz( str ) ;
    isz.seekg( 1,std::ios_base::beg ) ; std::cout << isz.fail() ;
    isz.seekg( 1,std::ios_base::cur ) ; std::cout << isz.fail() ;
    isz.seekg( 1,std::ios_base::end ) ; std::cout << isz.fail() ;
    return 0 ;
    }

I get 001 meaning the last seekg failed.
I the same with linux/g++ and win/MSVC.
I can't find such limitation in C++/stl documentation...
Any idea ?
Thx

Captain'Flam
  • 479
  • 4
  • 12
  • _"I get `001`"_ means what actually? – πάντα ῥεῖ Feb 25 '22 at 18:21
  • "001" is the string I get on stdout : it is the concatenation of the 3 " cout << isz.fail() ". Each one gives 0 (meaning false = not failed) or 1 (meaning true = failed). – Captain'Flam Feb 25 '22 at 18:23
  • 1
    Unrelated `fail` just returns true or false. If you inspect the individual fail bits (and `boolalpha` can make the output more readable) you can more precisely pinpoint the actual failure. – user4581301 Feb 25 '22 at 18:23
  • Did you mean to inspect the `eof()` flag rather than `fail()`?? I don't see why `isz.seekg( 1,std::ios_base::end )` should set the `fail()` flag? – πάντα ῥεῖ Feb 25 '22 at 18:28

1 Answers1

2

Sorry, I missed a point :
When I call seekg( 1,std::ios_base::end ), I try go 1 char after the end (which fails as it should).
I have to call seekg( -1,std::ios_base::end ) to go 1 char before the end.

Captain'Flam
  • 479
  • 4
  • 12