Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move the file pointer to next character through "ifstream" without "getting" any character, just like "fseek" does?

seekg uses ios as the second argument, and ios can be set to end or beg or some other values as shown here: http://www.cplusplus.com/reference/iostream/ios/

I just want the pointer to move to the next character, how is that to be accomplished through ifstream?

EDIT Well, the problem is that I want a function in ifstream similar to fseek, which moves the pointer without reading anything.

like image 324
Aquarius_Girl Avatar asked Jun 02 '11 10:06

Aquarius_Girl


3 Answers

ifstream fin(...);
// ...

fin.get(); // <--- move one character
// or
fin.ignore(); // <--- move one character
like image 79
Yakov Galka Avatar answered Nov 15 '22 02:11

Yakov Galka


Yes. Its called seekg() as you seem to already know?

std::ifstream is("plop.txt" );

// Do Stuff

is.seekg (1, std::ios::cur);  // Move 1 character forward from the current position.

Note this is the same as:

is.get();

// or 

is.ignore();
like image 40
Martin York Avatar answered Nov 15 '22 04:11

Martin York


Read the docs for seekg and use ios_base::cur as indicated there.

like image 24
Mat Avatar answered Nov 15 '22 04:11

Mat