How to move the file pointer to next character through "ifstream" without "getting" any character, just like "fseek" does?
20,144
Solution 1
ifstream fin(...);
// ...
fin.get(); // <--- move one character
// or
fin.ignore(); // <--- move one character
Solution 2
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();
Solution 3
Read the docs for seekg
and use ios_base::cur
as indicated there.
Author by
Aquarius_Girl
Arts and Crafts.stackexchange.com is in public beta now. Join us!
Updated on June 03, 2020Comments
-
Aquarius_Girl almost 4 years
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.
-
Aquarius_Girl almost 13 yearsThanks, but if I use the "ignore" will that effect the
getline
function which I want to use with pointer set to the next character? I mean will the ignored character be read by thegetline
then? -
Aquarius_Girl almost 13 yearsThanks again, but the doc says
current position within sequence
. And how is that going to move the pointer? I couldn't get the point. -
Aquarius_Girl almost 13 yearsI feared that! I want it to be read by the
getline
. I just want the pointer to be moved without "getting" anything. -
Mat almost 13 yearsThe docs says "current position in the stream buffer". With an offset of one and
::cur
, you'll move one byte forward. -
Aquarius_Girl almost 13 yearsLet me try that and see what happens.
-
Yakov Galka almost 13 years@Anisha: but that's impossible, getting is reading from the current position of the pointer! Maybe you should post what your problem is and ask for a proper solution.
-
Martin York almost 13 years@Anisha Kaul: When you move the pointer with seek or get or ignore you are moving the position of the pointer from which the next read will take place. You are in affect reading the next character (or moving over it) to set the position from which a read will take place. Explain the real problem. Your edit is explaining the solution you want to a problem that we don't understand. If you explain the real problem, what problem are you trying to solve that requires you to move forward one character, maybe we can suggest a better solution. i.e. we need more context.
-
code_dredd over 6 yearsIs there something else to this? I've used
std::ostream &s
to dos.seekp(-2, std::ios_base::cur);
in order to overwrite the last two chars by immediately doings << ">]";
, but the last 2 chars that should be overwritten (1 comma + 1 space) are still there and not overwritten; the">]"
chars do not show up in the stream..