在c++中读入值并存储在列表中

read in values and store in list in c++

本文关键字:列表 存储 c++      更新时间:2023-10-16

我有一个文本文件,数据如下:

name
weight 
groupcode
name
weight
groupcode
name
weight
groupcode

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

目前我有这个:

 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();
    }

我的工作与visual studio 2010专业!

因为我是c++初学者,所以可能有更好的方法!我愿意接受任何想法和建议

提前感谢!

#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:由于您想要将条目写入文件,因此只需将条目流式输出而不是将它们存储在vector中。当然你也可以重载操作符>>和<<

这里有个线索。您是否发现了代码与问题描述之间的不匹配?在问题描述中,数据以行、名称、权重、组代码和空白行为组。但是在您的代码中,每次循环只读取一行,您应该每次循环读取四行。像这样的

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
}

不是很完美,但希望能让你走上正确的道路。记住,代码的结构应该与问题描述的结构匹配。

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

使用此链接查看I/O api列表:http://msdn.microsoft.com/en-us/library/aa364232 (v = VS.85) . aspx

如果您想自己努力构建一个可用的程序,请阅读本文。如果你更喜欢通过实例学习并学习c++输入/输出的强大示例,我绝对建议你仔细阅读Simon的代码。

重要的是:当你写"char row[30];"时,你创建了一个30个字符的行缓冲区。在下一行中,您应该更改readFile。getline(行,50)调用readFile。getline(行,30)。否则,它将尝试读入50个字符,如果某人的名字超过30个字符,则缓冲区以外的内存将被损坏。所以,这是一个禁忌。;)

如果你想学习c++,我强烈建议你使用I/O标准库,而不是rplug建议的微软专用库。使用ifstream和getline是正确的。如果你想学习纯c++, Simon关于将字符数组转换为std::string的评论是正确的。

无论如何,john给出了关于围绕问题描述构建程序的好建议。正如他所说,您将需要在循环的每次迭代中读取四行。当您读取权重行时,您将希望找到一种方法从中获得数值输出(如果您坚持使用字符数组,请尝试http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/,或者尝试http://www.cplusplus.com/reference/clibrary/cstdlib/atof/获取非整数)。然后你可以把它加到总重量中。每次迭代,根据需要将数据输出到一个文件,一旦您的权重total>= 10000,您就知道该跳出循环了。

然而,您可能根本不想在while条件中使用getline:因为每次循环迭代必须使用getline四次,因此如果这样做,您要么必须使用类似Simon代码的东西,要么将结果存储在四个单独的缓冲区中(否则,您将没有时间读取权值并在读入下一行之前打印出该行!)。

相反,您还可以将循环结构为while(total <= 10000)或类似的结构。在这种情况下,可以使用四组if(readFile)。Getline (row, 30))在循环内,您将能够读取重量并打印出每组之间的内容。循环将在总权重超过10000的迭代后自动结束…但是,如果您到达文件的末尾,您也应该跳出它,否则您将永远陷入循环中。: p

祝你好运!