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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:58:17+00:00 2026-05-11T16:58:17+00:00

I quite recently learned about the C++ classes friend keyword and the uses in

  • 0

I quite recently learned about the C++ classes friend keyword and the uses in serialization and now I need some help in getting it to work.

I have no problem serializing my class to a file, it’s working great, however i’m having a hard time trying to read this file into a vector container. I’m sure I need a loop in my code that reads line by line, but since the class has different types I guess I can’t use std::getline() and also maybe that approach wouldn’t use the istream method i implemented?
A sample output file would be:

Person 1
2009
1

Person 2
2001
0

My code:

class SalesPeople {
    friend ostream &operator<<(ostream &stream, SalesPeople salesppl);
    friend istream &operator>>(istream &stream, SalesPeople &salesppl);

    private:
        string fullname;
        int employeeID;
        int startYear;
        bool status;
};

ostream &operator<<(ostream &stream, SalesPeople salesppl)
{
    stream << salesppl.fullname << endl;
    stream << salesppl.startYear << endl;
    stream << salesppl.status << endl;
    stream << endl;
    return stream;
}

istream &operator>>(istream &stream, SalesPeople &salesppl)
{
    stream >> salesppl.fullname;
    stream >> salesppl.startYear;
    stream >> salesppl.status;
    // not sure how to read that empty extra line here ?
    return stream;
}

// need some help here trying to read the file into a vector<SalesPeople>
SalesPeople employee;
vector<SalesPeople> employees;

ifstream read("employees.dat", ios::in);
if (!read) {
   cerr << "Unable to open input file.\n";
   return 1;
}

// i am pretty sure i need a loop here and should go line by line 
// to read all the records, however the class has different
// types and im not sure how to use the istream method here.

read >> employee;
employees.push_back(employee);

By the way, I know that the Boost library has a great serialization class, however I’m trying to learn how serialization would work using the STL library for now.
Thanks a lot in advance for any help that you can give me and for getting me in the right track!

  • 1 1 Answer
  • 2 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-11T16:58:17+00:00Added an answer on May 11, 2026 at 4:58 pm

    It looks like you pretty much have all the code you need already! I copied your code and compiled it with some changes to read the SalesPeople in from a file in a loop. I will include the changes below, but since this is for your homework, you may just want to read and think about the following hints before looking at the code.

    • For reading the SalesPeople in a
      loop, I would recommend that you take
      a look at this FAQ. It has an
      example of almost exactly what you
      need. FAQ 15.4 will also help
      you, I believe.

    • For your question on how to handle
      the extra empty line when reading
      from the file, check out this
      link. You can very simply
      extract whitespace this way.

    • As jfclavette suggested, I would
      recommend looking into
      std::getline for reading in the
      SalesPerson’s full name, since you
      need everything on that line into one
      string.

    I have one question for you, though: what about the employeeID? I notice that it is being ignored in your sample code. Is that on purpose?

    And now, if you still need help, you can check out the code I wrote to get this to work:

    istream &operator>>(istream &stream, SalesPeople &salesppl)
    {
        //stream >> salesppl.fullname;
        getline(stream, salesppl.fullname);
        stream >> salesppl.startYear;
        stream >> salesppl.status;
        // not sure how to read that empty extra line here ?
        stream >> ws;
        return stream;
    }
    
    while(read >> employee)
    {
        // cout << employee; // to verify the input, uncomment this line
        employees.push_back(employee);
    }
    

    Also, as jfclavette suggested, it may not be a bad idea to add some input validation (check the stream status after reading from it and verify that it is still good). Although I would recommend using the while() loop for the reasons stated in FAQ 15.5.

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

Sidebar

Related Questions

I'm quite new to this Git thing and need a little help. I recently
Quite recently, I asked a question about debugging a seg fault: What are some
I've recently started getting into data analysis and I've learned quite a bit over
I have recently been doing some work that has been quite in depth, i
Having recently learned of the DebuggerDisplay attribute, I've found it quite useful. However, one
Recently I spent quite some time writing various Visual Studio Extensions projects. Even though
I have been studying python for quite sometime now and very recently I decided
I add this problem for quite some time when using NetBeans. Recently I add
I've been doing quite large application recently with Java - Swing. Now I'd like
I've recently started working with RoR for some projects and I quite like the

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.