Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8676573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:13:50+00:00 2026-06-12T20:13:50+00:00

After I read a line from a file using ifstream, is there a way

  • 0

After I read a line from a file using ifstream, is there a way to bring the stream back to the beginning of the line I just read, conditionally?

using namespace std;
//Some code here
ifstream ifs(filename);
string line;
while(ifs >> line)
{
   //Some code here related to the line I just read

   if(someCondition == true)
   {
    //Go back to the beginning of the line just read
   }
   //More code here
} 

So if someCondition is true, the next line read during the next while-loop iteration will be the same line I just read right now. Otherwise, the next while-loop iteration will use the following line in the file. If you need further clarification, please don’t hesitate to ask. Thanks in advance!

UPDATE #1

So I tried the following:

while(ifs >> line)
{
   //Some code here related to the line I just read
   int place = ifs.tellg();
   if(someCondition == true)
   {
    //Go back to the beginning of the line just read
    ifs.seekg(place);
   }
   //More code here
}

But it doesn’t read the same line again when the condition is true. Is an integer an acceptable type here?

UPDATE #2: The Solution

There was an error in my logic. Here is the corrected version that works as I want it to for any of those that are curious:

int place = 0;
while(ifs >> line)
{
   //Some code here related to the line I just read

   if(someCondition == true)
   {
    //Go back to the beginning of the line just read
    ifs.seekg(place);
   }
  place = ifs.tellg();
   //More code here
}

The call to tellg() was moved to the end because you need to seek to the beginning of the previously read line. The first time around I called tellg() and then called seekg() before the stream even changed, which is why it seemed like nothing changed (because it really hadn’t). Thank you all for your contributions.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T20:13:51+00:00Added an answer on June 12, 2026 at 8:13 pm

    There is no direct way to say “get back to the start of the last line”. However, you can get back to a position you kept by using std::istream::tellg(). That is, before reading a line you’d use tellg() and then seekg() to get back to the position.

    However, calling the seek functions frequently is fairly expensive, i.e., I would look at removing the requirement to read lines again.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program that load data from a file using std::ifstream and store
I've got a function that should read from file line by line, the reading
I need to read line by line from text file (log files from server)
... after having just read http://www.cocoadev.com/index.pl?CocoaInsecurity ... I am curious to know about your
How do I read data from a file if my file is like this
Matlab is failing to read in the specified number of elements from a file.
I have the following basic code to read a text file from a StreamReader:
I am reading from a text file line by line. StreamReader reader = new
gcc 4.5.1 c89 I am using the following code to read in a line
I am trying to understand how a file position indicator moves after I read

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.