C++ 读入值并存储在c++;

C++ 读入值并存储在c++;,c++,list,stream,text-files,C++,List,Stream,Text Files,我有一个包含如下数据的文本文件: name weight groupcode name weight groupcode name weight groupcode 现在我想把所有人的数据写入一个输出文件,直到达到10000公斤的最大重量 目前我有: void loadData(){ ifstream readFile( "inFile.txt" ); if( !readFile.is_open() ) {

我有一个包含如下数据的文本文件:

name
weight 
groupcode

name
weight
groupcode

name
weight
groupcode
现在我想把所有人的数据写入一个输出文件,直到达到10000公斤的最大重量

目前我有:

 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();
    }
void loadData(){
ifstream readFile(“infle.txt”);
如果(!readFile.is_open())
{

如果有两个文件指针,试着读取输入文件并继续写入o/p文件。同时有一个计数器并不断增加权重。当权重>=10k时,中断循环。到那时,o/p文件中就有了所需的数据

使用此链接查看I/O API列表:

有两个文件指针,尝试读取输入文件并继续写入o/p文件。同时有一个计数器并随着重量不断增加。当重量>=10k时,中断循环。到那时,o/p文件中就有了所需的数据

使用此链接查看I/O API列表:
好的,这里有一个线索。你看到你的代码和你的问题描述之间的不匹配吗?在你的问题描述中,你有四行一组的数据,包括姓名、体重、组码和一个空行。但是在你的代码中,你每次循环只读一行,你应该读四行g> 每次在你的循环中都会有一行这样的东西

char name[30];
char weight[30];
char groupcode[30];
char blank[30];
while (readFile.getline (name, 30) && 
    readFile.getline (weight, 30) && 
    readFile.getline (groupcode, 30) && 
    readFile.getline (blank, 30)) 
{
    // now do something with name, weight and groupcode
}

很长一段时间都不完美,但希望它能让你走上正轨。记住,代码的结构应该与问题描述的结构相匹配。

好吧,这里有一条线索。你看到代码和问题描述之间的不匹配吗?在问题描述中,你有四组数据trong>行、名称、重量、组码和一个空行。但是在代码中,您每次循环只读取一行,您每次循环应读取四行。类似于

char name[30];
char weight[30];
char groupcode[30];
char blank[30];
while (readFile.getline (name, 30) && 
    readFile.getline (weight, 30) && 
    readFile.getline (groupcode, 30) && 
    readFile.getline (blank, 30)) 
{
    // now do something with name, weight and groupcode
}
#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;
    }
}
虽然很长一段路都不完美,但希望它能让你走上正轨。记住,代码的结构应该与问题描述的结构相匹配。

\include
#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;
    }
}
#包括 #包括 #包括 #包括 结构条目 { 条目() :重量() { } std::字符串名; 整数重量;//kg std::字符串组_代码; }; //data.txt的内容 //(无前导空格) // //约翰 // 80 //摔跤手 // //乔 // 75 //牛仔 int main() { std::ifstream流(“data.txt”); 如果(流) { std::向量条目; 常数限值总重量=10000;//kg int总重量=0;//kg 入口电流; while(std::getline(stream,current.name)&& 流>>当前重量&& stream.ignore(std::numeric_limits::max(),'\n')&&//跳过包含权重的行的其余部分 std::getline(流、当前.组\代码)) { 条目。推回(当前); 总重量+=当前重量; 如果(总重量>限制总重量) { 打破 } //忽略空行 忽略(std::numeric_limits::max(),'\n'); } } 其他的 { 标准::cerr和操作员
#包括
#包括
#包括
#包括
#包括
结构条目
{
条目()
:重量()
{ }
std::字符串名;
整数重量;//kg
std::字符串组_代码;
};
//data.txt的内容
//(无前导空格)
//
//约翰
// 80
//摔跤手
// 
//乔
// 75
//牛仔
int main()
{
std::ifstream流(“data.txt”);
如果(流)
{
std::向量条目;
常数限值总重量=10000;//kg
int总重量=0;//kg
入口电流;
while(std::getline(stream,current.name)&&
流>>当前重量&&
stream.ignore(std::numeric_limits::max(),'\n')&&//跳过包含权重的行的其余部分
std::getline(流、当前.组\代码))
{
条目。推回(当前);
总重量+=当前重量;
如果(总重量>限制总重量)
{
打破
}
//忽略空行
忽略(std::numeric_limits::max(),'\n');
}
}
其他的
{

STR::CURR和操作符

如果你想通过自己的努力来构建一个工作程序,请阅读此。如果你愿意学习示例并学习C++输入输出的强大例子,我绝对建议对西蒙的代码进行深入研究。 第一件事:您在编写“char row[30];”时创建了一个包含30个字符的行缓冲区 在下一行中,您应该将readFile.getline(第50行)调用更改为readFile.getline(第30行)。否则,它将尝试读取50个字符,如果某人的名字超过30个,则缓冲区之外的内存将被损坏。因此,这是不允许的

<>如果你想学习C++,我强烈建议你使用I/O标准库而不是RPLUSG建议的微软特定库。你在IFFString和GETLIN的正确的轨道上。如果你想学习纯C++,西蒙在他关于STD::字符串的字符数组的切换中有正确的想法。

无论如何,john对围绕问题描述构建程序给出了很好的建议。正如他所说,在循环的每次迭代中,您都需要阅读四行。当您阅读权重行时,您需要找到一种方法从中获得数字输出(如果您坚持使用字符数组,请尝试,或尝试非整数)。然后您可以将其添加到运行的总重量中。每次迭代,根据需要将数据输出到文件中,一旦您的总重量>=10000,您就知道要打破循环

但是,您可能根本不想在while条件中使用getline:因为