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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:28:02+00:00 2026-05-31T10:28:02+00:00

The structure is pretty simple: Main point List item – 5 List item –

  • 0

The structure is pretty simple:

  1. Main point
    1. List item – 5
    2. List item – 6
    3. …
  2. Main point
    1. List item – 2
    2. List item – 3
    3. …

To make it easier to read I have added some delimiters, Here is the actual file:

>Point one
item 1      | 150
item 2      | 20
>Point two
item 1      | 150
item 2      | 10

And I wrote a C++ function which should read it. But I have tangled somewhere in my own logic and made a mistake. Could find it for me?

  ifstream fileR("file.txt"); 

  int i = 0;
  getline(fileR, sTemp);
  do {
    if(!sTemp.empty() && sTemp[0]==DELIM){
        R[0][i] = sTemp.substr(1);
        i++;
    }else{
        j = 0;
        do {
            if(!sTemp.empty() && sTemp[0] != DELIM){
                string::const_iterator pos = find(sTemp.begin(), sTemp.end(), '|');
                string name(sTemp.begin(), pos);
                string a_raw(pos + 1, sTemp.end());
                a_raw = trim(a_raw);
                double amount(atof(a_raw.c_str()));
                R[j+1][i].set(trim(name), amount);
                j++;
            }else{
                break;
            }
        }while(getline(fileR, sTemp));
    }
  }while(getline(fileR, sTemp));

Where the read values are handed is not important because I tried to simplify this function because it is actually some containers with dynamic arrays. I have tested them and they do work fine. So there is a problem with reading. It seam to read the first value fine but afterwards it makes some kind of mess.
If you think my attempt is a complete disaster I would welcome hints of how to make a really working function.

EDIT:
I have a good night sleep and I have fixed it. Here is a wokrking version:

  bool read =  true;
  for(int i = 0; i<n; i++) {
    if(read){getline(fileR, sTemp);}else{ read = true; }
    if(!sTemp.empty() && sTemp[0]==DELIM){
        R[i].setName(sTemp.substr(1));
        i--;
    }else{
        R[i].toFirstSubpoint();
        do {
            if(!sTemp.empty() && sTemp[0] != DELIM){
                string::const_iterator pos = find(sTemp.begin(), sTemp.end(), '|');
                string name(sTemp.begin(), pos);
                string a_raw(pos + 1, sTemp.end());
                a_raw = trim(a_raw);
                double amount(atof(a_raw.c_str()));
                R[i].setSubpoint(trim(name), amount);
                R[i].toNextSubpoint();
            }else{
                read = false;
                break;
            }
        }while(getline(fileR, sTemp));
    }

The my most important mistake was that I forgot that if after condition is checked the loop goes to next index and does not execute the other (else) condition of it.

The answer below is actually better way to do it is much more simple and easier to understand yet I could not have used vectors this time and I am guessing that my script should be a bit quicker since it doe not use them. Anyhow next time I will run into similar situation I will use the solution below.

  • 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-31T10:28:04+00:00Added an answer on May 31, 2026 at 10:28 am

    You should probably consider using a parsing library like boost::spirit, however if the file format is optional there are plenty of thing you can do to make it easier to parse without such measures,

    Consider the following date layout

    MainPoint1 iname1 ivalue1 iname2 ivalue2 iname3 ivalue3
    MainPoint2 iname1 ivalue1 iname2 ivalue2
    

    This could easily be loaded with the following.

    struct item{
        std::string name;
        double value;
    };
    
    std::ostream& operator<<(std::ostream& os, const item& i)
    {
        return os << i.name << " " << i.value;
    } 
    
    std::istream& operator>>(std::istream& is, const item& i)
    {
        return is >> i.name >> i.value;
    } 
    
    struct point{
        std::string value;
        std::vector<item> items;
    };
    
    
    std::istream& operator>>(std::istream& is, point& p)
    {
         std::string line;
         std::getline(is, line);
         std::stringstream ss(line);
         ss >> p.value;
         p.assign(
             std::istream_iterator<item>(ss),
             std::istream_iterator<item>());
         return is;
    }
    
    
    std::ostream& operator<<(std::istream& os, const point& p)
    {
         os << p.value << "   ";
         std::copy(p.item.begin(), p.items.end(),
             std::ostream_iterator<item>(os, " "),
         return os;
    }
    
    int main()
    {
        /*
        ** Deserailise
        */
        std::ifstream in_file("infile.dat");
        std::vector<point> points(
             std::istream_iterator<point>(in_file),
             std::istream_iterator<point>());
    
    
        /*
        ** Serailise
        */
        std::ofstream out_file("outfile.dat");
        std::copy(points.begin(), points.end()
             std::ostream_iterator<points>(out_file, "\n"));
    }
    

    This is wholey untested and uncompiled, but conceptually it should work.

    EDIT: code changed to meet item with name and value.

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

Sidebar

Related Questions

I have a text file with few hundred lines, the structure is pretty simple.
I have a pretty simple DB structure. I have 12 columns in a single
I have a pretty simple form with some fields from a doctrine model. $this->widgetSchema['fields']
Basically I do have pretty simple database that I'd like to index with Lucene.
Assuming I have a simple structure that looks like this: public class Range {
This question should be pretty simple. I have a php file in a directory
I'm pretty new to MATLAB and I have a simple question. What if I
I have a pretty simple profile page where users can upload images and videos.
I'm pretty new to the Entity framework and I'm modelling this simple structure: With
Given a pretty basic source tree structure like the following: trunk ------- QA |--------

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.