0

I'm trying to use seekg for the first time. It says in the documentation it's arguments could be:

istream& seekg ( streampos pos );

istream& seekg ( streamoff off, ios_base::seekdir dir );

From what I understood, 'streampos pos' could be an int? I saw examples like "seekg(0)" However when try to compile this it says I'm using invalid arguments:

void function(int pos){
ifstream reader("data.dat");
if(!reader.is_open())
    return 0;
int posinArchive = pos * 74;
reader.seekg(posinArchive);

I even tried to cast it to streampos directly:

  void function(int pos){
ifstream reader("data.dat");
if(!reader.is_open())
    return 0;
int posinArchive = pos * 74;
reader.seekg((streampos)posinArchive);

same thing. seekg(0), nothing too. What's wrong?

5
  • Post the text of the error you're getting?
    – reuben
    Jul 5, 2012 at 10:25
  • invalid arguments' Candidates are: std::basic_istream<char,std::char_traits<char>> & seekg(?) std::basic_istream<char,std::char_traits<char>> & seekg(long long int, enum std::_los_Seekdir)
    – John smith
    Jul 5, 2012 at 10:33
  • From the error, it almost sounds like the compiler isn't finding a declaration/overload for the first variant of the method that you have listed above (from the docs). I know it might be an odd question, but is there any chance your code can't access a definition for streampos? Alternatively, it would be interesting to see if you'd have more luck with the other variant...
    – reuben
    Jul 5, 2012 at 10:37
  • if I type seekg (0, ios::end); it does work.Also when I try to cast, streampos turns green so I think it knows what it is.
    – John smith
    Jul 5, 2012 at 10:45
  • Okay, it sounds like you have a workaround, then. To figure out what's really going on, you might consider running your source file through the preprocessor to confirm the seekg declarations are getting pulled in correctly from the header files you're including...
    – reuben
    Jul 5, 2012 at 10:47

1 Answer 1

0

The first version

stream& seekg ( streampos pos );

is supposed to be used when you want to seek to a position that you have previously got from a call to tellg.

If you want to seek to position 0, you have to use seekg(0, ios::beg) to get to zero offset from the beginning of the file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.