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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:47:55+00:00 2026-06-04T14:47:55+00:00

I need to read from a file and then break the line into 3

  • 0

I need to read from a file and then break the line into 3 strings.
The format is:
A first_The Secod_Third (three underscores)
It’s homework, and they suggest us using getline and ignore.
so I have:

    main()
    ifstream inf("file.txt")
    while(inf)
    {inf >> class1;
    cout << class1;
    }
    class THECLASS
    {string a, b, c;
    public:
    friend void operator>>(ifstream &inf, THECLASS &class1)
    {getline(inf, class1.a, '_');
    inf.ignore();
    inf.ignore();
    [if I put getline class1.b, the whole line will go into it, overwriting .a]
    }

and in operator<<, I have

    os << class1.a << class1.b;
    return os;

But all I get when I cout << class1 are all three fields of input file without _, each on a new line.
When I tried using get() function, compiler won’t recognize it even though I declared fstream.
What is the general algorithm of doing it?

  • 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-04T14:47:57+00:00Added an answer on June 4, 2026 at 2:47 pm

    Hope this can help u.

    // assume the content of infile : 
    //First1_xxSecond1_xxThird1
    //Firts2_xxSecond2_xxThird2
    //First3_xxSecond3_xxThird3
    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;
    class MyClass
    {   
    public:
        string a, b, c;
    };
    ifstream& operator>>(ifstream &inf, MyClass& class1)
    {
        getline(inf, class1.a, '_');    //"First" into class1.a
        inf.ignore(2,EOF);              //skip "xx"
        getline(inf, class1.b, '_');
        inf.ignore(2, EOF);
        getline(inf, class1.c);
        return inf;
    }
    ostream&  operator<<(ostream& out, const MyClass& class1)
    {
        out<< class1.a << "_"<< class1.b<< "_" << class1.c;
        return out;  
    }
    int main () { 
        MyClass class1;
        ifstream  stream("test.txt");
        while(stream.rdstate()  != ifstream::eofbit)
        {
            stream >> class1;
            cout << class1 <<"\n";  //First1_Second1_Third1 for the first loop.ect
        }
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write something into a txt file and read the contents, then
I need to read one block from file and then need to match the
I need to read a BufferedImage from file, which doesn't use DataBufferInt (as normally),
I need to read a file from the local file system using JSP, save
I need to read the data from a file that can be either comma
I need to read from either an array or a file (preferably a file
Problem: I need to write/read objects from a file.This because I need to write/read
As part of an assignment, I need to read data from a binary file
I need to read some data from an input file and plot a graph
I need to read data from a legacy database file produced by Visual Basic

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.