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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:31:25+00:00 2026-06-14T12:31:25+00:00

so this is my text file: Computer Science 4 345848534 1 Ivan Ivanov Georgi

  • 0

so this is my text file:

Computer Science
4
345848534
1 
Ivan Ivanov
Georgi Georgiev
Plamen Angelov
======
Oreily
5th avenue, London
44384208434
*****************
Biology
2
58934673
0 
Georgi Ivanov
Nikolay Stamatov
Jack Johnson
======
Head first
Stanley Str 4, Manchester
449348344

And this is my function to read from the file:

void ApprovedBook::read_from_file() {
fstream file;
string heading; int edition; long ISBN = 0L; bool isApproved = 0; // temp values   for each book 
string name; string address; long telephone = 0L; // temp values for manufacturer of the book
vector<string> authors; // temp vector for authors of the book  
string line;

file.open("books.txt", ios::in | ios::app);

while (file.good()) {
    for (int i=1; i<=NUM_ITEMS; i++) { 
        getline(file, line);
        switch (i) {
        case 1: 
            heading = line; break;
        case 2:
            edition = atoi(line.c_str()); break;
        case 3:
            ISBN = atol(line.c_str()); break;
        case 4:
            isApproved = (bool) atoi(line.c_str()); break;
        }
    }

    getline(file, line);
    while (line != "======") {
        authors.push_back(line);
        getline(file,line);
    }

    int i = 1;
    getline(file, line);
    while (line != "*****************") {
        switch (i) {
        case 1: 
            name = line; break;
        case 2:
            address = line; break;
        case 3: 
            telephone = atol(line.c_str()); break;
        }

        getline(file, line);
        i++;
    }

    Manufacturer m(name, address, telephone);
    ApprovedBook a(heading, authors, edition, ISBN, m, isApproved);
    cout << a << endl;
    authors.clear();
}

file.close();
}

So I separate the information needed to construct an “ApprovedBook” object with the *** line. The lines between “=====” and the stars are needed to construct a “Manufacturer” object which is a property of ApprovedBook as well. So I read the first piece information and output the object with << (i have predefined the operator for the class). But after that the application freezes and doesn’t seem to read the next piece of information which is below the the stars. What’s the problem with that? Is the file.good() condition enough or maybe some more advanced check is needed ?

  • 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-14T12:31:27+00:00Added an answer on June 14, 2026 at 12:31 pm

    I replaced file.good with while (line != “”) and that worked fine for me.

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

Sidebar

Related Questions

I have text file of 550mb.When I run this code computer hangs after 10
Having this text file : Breathing 1:-29.9396 9:-32.0895 13:-35.6787 2:-36.8527 17:-37.3622 8:-38.0584 14:-38.5265 18:-38.7567
I have got this text file with latitude and longitude values of different points
I have a text file exported from a Foxpro (Dos-based) program, but this text
this is probably pretty simple, but I've got this text file containing a bunch
This is the data in my text file: (I have shown 10 rows out
I have a text file like this: A B C And each element has
I have text file with some text information and i need to split this
I have a text file looks like this : 100 50 20 90 4.07498
if I have a text file like this : this is line one This

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.