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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:46:13+00:00 2026-06-01T06:46:13+00:00

the following code is not behaving like I would expect. Please help me understand

  • 0

the following code is not behaving like I would expect. Please help me understand how it works.

#include <algorithm>
#include <iterator>
#include <fstream>
#include <vector>
#include <string>
using namespace std;

struct user
{
        string name;
        string age;
        string id;
};

istream& operator>>(istream& is, user& s)
{
        getline(is, s.name, ':');
        getline(is, s.age, ':');
        getline(is, s.id);

        return is;
}

int main(int argc, char* argv[])
{
        ifstream file("file.txt");
        vector<user> vec;
        copy(istream_iterator<user>(file), istream_iterator<user>(), back_inserter(vec));

        return 0;
}

My custom operator>> is called twice but I would expect it to be called only once because the contents are:

John:forty:21-5821-0

  • 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-01T06:46:14+00:00Added an answer on June 1, 2026 at 6:46 am

    In general, to read in an entire file, you read until a read fails. Then you know either something went wrong, or you got them all. Either way, you can’t know you reached the end of the file until you fail to read. Since the first one succeeds, it must try a second time, to find out if there is a second element. The psudocode for this is

    while(in_stream >> object) {
       myvector.push_back(object);
    }
    

    Also note that this is the “idiomatic” way to read in an entire file of values. If you’re checking for eof, fail, or bad, you’re code is probably wrong.

    That said, your istream& operator>>(istream& is, user& s) function is just fine. The second time it is called, the first getline will fail, setting the stream to a bad state (eof), the next two getlines will fail as well, and it will return the stream, and everything works perfectly. Just remember that any or all of those variables may hold complete nonsense, since the read failed.

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

Sidebar

Related Questions

Why does the following code not work #include <iostream> #include <fstream> #include <stdio.h> #include
I am not sure why the following code is not behaving the way I
Why does the following code not allow foo(ptr) to be called ? #include <boost/scoped_ptr.hpp>
can anybody explain why the following bash code involving compound operators is not behaving
I am using the following code to send email in codeigniter and would like
Why does the following code not work? #include <iostream> #include <string> int main(){ char
Why is the following code not compiling and how would it be possible to
Why does the following code not work as I was expecting? <?php $data =
Why does the following code NOT give an error, nor any type of a
Why is the following code not working? if(EventLog.Exists(Foo)) { EventLog.Delete(Foo); } if(EventLog.Exists(Foo) == false)

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.