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 6960825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:27:05+00:00 2026-05-27T15:27:05+00:00

I have 2 questions: What happens to s if I again do a getline(in,line)

  • 0

I have 2 questions:

  1. What happens to s if I again do a “getline(in,line)” after the while loop ends?

    ifstream in("string.txt");
    string s, line;
    s = "";
    while(getline(in,line))
    {
    
        s = s + line + "\n";
    
    }
    
    cout<<s<<endl<<"******************************************"<<endl;
    
  2. The getline() function: everytime it is called, does it goes to the “next” line of the ifstream “in” object in the above code? If so what happens when the while loop ends and I call the same function again? (almost the same as first question, just a subtle difference)

  • 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-05-27T15:27:06+00:00Added an answer on May 27, 2026 at 3:27 pm

    What happens to s if I again do a “getline(in,line)” after the while loop ends?

    The loop ends when std::getline() returns something that resembles a boolean false. When you look at the signature of the function you will see that it returns the stream. Streams have an implicit conversion to something boolean-like that resembles their state. If if(stream) evaluates the stream to false, then that means that the stream is in a bad state. This could be the EOF flag set or one of the error flags, or both.
    Any attempt to use a stream that is in a bad state will fail. Nothing will be read.

    The getline() function: everytime it is called, does it goes to the “next” line of the ifstream “in” object in the above code? If so what happens when the while loop ends and I call the same function again? (almost the same as first question, just a subtle difference)

    This has nothing to do with std::getline(). The position in the opened file is a property of the (file) stream. Every time you (re-)open the file for reading (without passing extra parameter for setting the position), the position is set to the beginning of the stream (file).

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

Sidebar

Related Questions

Java 6 API question. Does calling LockSupport.unpark(thread) have a happens-before relationship to the return
I have questions about System.Threading.ThreadStart Class : where can I find its specifications (
I am going to be making a small user system but I have questions.
I want to create some simple navigation in sencha touch 2. I have questions
Similar questions have been asked, but nothing exactly like mine, so here goes. We
similar questions have been asked before but I cant find an exact match to
Similar questions have been asked but cannot find a solution that works well. What
I have 2 questions: suppose we have one entity named class and another called
I have two questions... Here is a really simple example script which causes an
I have two questions concerning authentication of a intranet website and how to go

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.