开发者

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

开发者 https://www.devze.com 2023-03-09 19:22 出处:网络
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/

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.


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

fin.get(); // <--- move one character
// or
fin.ignore(); // <--- move one character


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();


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


I guess peek() and unget() could be useful too.

Use peek() to peek next character so that getline will work as you have wanted.

Use unget to put the character back to your buffer in case you have used get() method.

0

精彩评论

暂无评论...
验证码 换一张
取 消