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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:05:36+00:00 2026-05-25T03:05:36+00:00

i have a text file with data like the following: name weight groupcode name

  • 0

i have a text file with data like the following:

name
weight 
groupcode

name
weight
groupcode

name
weight
groupcode

now i want write the data of all persons into a output file till the maximum weight of 10000 kg is reached.

currently i have this:

 void loadData(){
            ifstream readFile( "inFile.txt" );
            if( !readFile.is_open() )
        {
            cout << "Cannot open file" << endl;
        }
            else
            {
                    cout << "Open file" << endl;
            }

            char row[30]; // max length of a value
            while(readFile.getline (row, 50))
            {
                    cout << row << endl;
                    // how can i store the data into a list and also calculating the total weight?
            }
            readFile.close();
    }

i work with visual studio 2010 professional!

because i am a c++ beginner there could be is a better way! i am open for any idea’s and suggestions

thanks in advance!

  • 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-25T03:05:37+00:00Added an answer on May 25, 2026 at 3:05 am
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    #include <limits>
    
    struct entry
    {
        entry()
            : weight()
        { }
    
        std::string name;
        int weight; // kg
        std::string group_code;
    };
    
    // content of data.txt
    // (without leading space)
    //
    // John
    // 80
    // Wrestler
    // 
    // Joe
    // 75
    // Cowboy
    
    
    int main()
    {
        std::ifstream stream("data.txt");
        if (stream)
        {
            std::vector<entry> entries;
    
            const int limit_total_weight = 10000;   // kg
            int total_weight = 0;                   // kg
    
            entry current;
            while (std::getline(stream, current.name) &&
                   stream >> current.weight &&
                   stream.ignore(std::numeric_limits<std::streamsize>::max(), '\n') &&  // skip the rest of the line containing the weight
                   std::getline(stream, current.group_code))
            {
                entries.push_back(current);
    
                total_weight += current.weight;
                if (total_weight > limit_total_weight)
                {
                    break;
                }
    
                // ignore empty line
                stream.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            }
        }
        else
        {
            std::cerr << "could not open the file" << std::endl;
        }
    }
    

    Edit: Since you wannt to write the entries to a file, just stream out the entries instead of storing them in the vector. And of course you could overload the operator >> and operator << for the entry type.

    • 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 that contains a data like ID Name Path IsTrue
Suppose I have a text file with data separated by whitespace into columns. I
I have a text file containing the data in following format 12345 Abdt3 hy45d
I'm probably doing this all wrong. I have a text file full of data
i have a text file with the following data: Calculated Concentrations 30.55 73.48 298.25
I have a text file that contains the following data. The first line is
I have a huge text file with lots of lines like: asdasdasdaasdasd_DATA_3424223423423423 gsgsdgsgs_DATA_6846343636 .....
I have Text file that contains data separated with a comma , . How
I have a text file which contains data seperated by '|'. I need to
Currently I have a a text file with data at the first row is

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.