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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:39:53+00:00 2026-05-27T13:39:53+00:00

I have the following class class Film { Person authors[5]; //This will actually include

  • 0

I have the following class

class Film {
    Person authors[5]; //This will actually include only the director
    string title;
    string producer;
    int n_authors;
    int year;
    int running_time;
    Person actors[5];
    int n_actors;
}

And the following file format (don’t ask me why I use this, I MUST use this format)

Stanley
Kubrick  
#          
2001: A Space Odissey
*
1968
161
Keir
Dullea
Gary 
Lockwood
#

The # indicates the end of a list (in this case a ‘Person’ class), while the * a missing field (in this case the producer, btw the producer field must be filled with an * in the class).
The class Person consists of Name and Surname and has an overloaded operator >> that calls:

void load(ifstream& in) {
    getline(in,name);
    getline(in,surname);
}

What’s the best method to parse this file structure? I can’t use regular expressions or anything more advanced than ifstream. My concern is on how (and where in the code) to detect the end of file and the end of a list of people.

  • 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-27T13:39:54+00:00Added an answer on May 27, 2026 at 1:39 pm

    The standard line reading idiom:

    #include <fstream>   // for std::ifstream
    #include <sstream>   // for std::istringstream
    #include <string>    // for std::string and std::getline
    
    int main()
    {
        std::ifstream infile("thefile.txt");
        std::string line;
    
        while (std::getline(infile, line))
        {
            // process line
        }
    }
    

    Where it says “process line” you should add some logic that tracks the current state of the parser.

    For your simple application you could proceed in bits, reading lists and tokens as specified by the format. For instance:

    std::vector<std::string> read_list(std::istream & in)
    {
        std::string line;
        std::vector<std::string> result;
    
        while (std::getline(in, line))
        {
            if (line == "#") { return result; }
            result.push_back(std::move(line));
        }
    
        throw std::runtime_error("Unterminated list");
    }
    

    Now you can say:

    std::string title, producer, token3, token4, token5, token6;
    
    std::vector<std::string> authors = read_list(infile);
    
    if (!(std::getline(infile, title)    &&
          std::getline(infile, producer) &&
          std::getline(infile, token3)   &&
          std::getline(infile, token4)   &&
          std::getline(infile, token5)     ) )
    {
        throw std::runtime_error("Invalid file format");
    }
    
    std::vector<std::string> actors = read_list(infile);
    

    You can use std::stoi to convert tokens 3 – 5 to integers:

    int year = std::stoi(token4);
    int runtime = std::stoi(token5);
    

    Note that the n_authors and n_actors variables are redundant, since you have self-terminated lists already. You can or should use the variables as an integrity check if you like.

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

Sidebar

Related Questions

I have following simple class: @interface Article: NSObject { NSString *title; } @property (copy,
I have the following class public class MenuVeiculo { public string Nome { get;
I have the following class A. public class A { public string Name {
Lets say i have following class public class abc { int id; string name;
this is my first question I Have following class class ProDetection { public function
i have following class with overloading operators #include<iostream> #include<algorithm> #include<math.h> using namespace std; class
I have following class hierarchy: class Base { // This class cannot be modified
If have following class public abstract class MyBaseClass : BaseClass { public override string
I have this form which relates to an object of class Film, which has
I have following string Class (102) (401) and Class (401) i want to find

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.