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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:59:19+00:00 2026-06-15T18:59:19+00:00

I have several classes that are containers of various data that I read from

  • 0

I have several classes that are containers of various data that I read from / write to files. Usually the files have one entity per line (comma separated fields), but sometimes an entity is spread out among several lines.

I am trying to come up with a correct way of reusing the code reading and writing to a file. I could just have separate Read() and Write() functions in each class that deal with fstreams, all the checkings and strings formatting, but I feel like there should be a better way of reusing some code.

EDIT:

I realize I wasn’t specific enough about my problem. What stopped me from implementing it the way that some of the answers/comments have suggested, is that I have multi-line entities. So, I cannot just pass a line read from a file to a class to parse it. Some times I need to pass several lines.

When an entity is multi-line, usually it has an id field at the beginning of the line, so repeating ids mean that I need to read several lines.

For example, one file can be a polygon, then it’d have two coordinates per line, and this case is simple. But another file can be a set of polygons, then it’d have a polygon id plus two coordinates in each row. Like that:

id, x, y
0, 1, 2
0, 5, 0
0, -1, -1
1, 4, 4
1, 0, 8
1, -4, 4
1, 0, 0

EDIT 2:

While I was describing my problem in the first edit I realized what to do with my multi-line entities. I can still pass the read lines one by one to each class and make it populate unfinished entities. This seems obvious to me now, as always the problem becomes clear as soon as I try to explain it to others. Thanks for your comments!

  • 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-15T18:59:20+00:00Added an answer on June 15, 2026 at 6:59 pm

    If the data for the various containers is similar enough you should be able to make a single class for file IO. Pass it the type of object you want to write/populate.

    Make each class implement an interface which defines two methods; one which takes the current line or an array of strings (after the line has been split) and sets all the objects values and another which returns a string with all the data as a comma separated string (this is for writing). The multi line situation will be more difficult to handle and it may be worth while to treat it as a special case.

    The file IO class should only open/close, read/write, split lines on commas, and call methods on the object it’s passed. The file IO class should be passed pointer of the interfaces type.

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

Sidebar

Related Questions

This is for a web project so i have several classes that inherit from
I have several classes, that all derives from SuperClass. When the classes are created,
I have several small classes that are in a single file in /app/models, similar
I have several obj-c classes, each of which require a number of constants that
I have some methods that are used by several classes. My idea was to
I have a class that has several member classes as attributes. The constructor of
I have several classes with static const data members. I would like to know
i have a little problem implementing some serialization/deserialization logic. I have several classes that
I have several classes that each subclass an Operator. An Operator has several inputs
I have several pages and one stylesheet. The page bodies get two classes: Their

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.