简体   繁体   English

如何从文件中读取两个字符串和数字数组并将它们存储在对象向量中

[英]How to read two strings and array of numbers from a file and store them in a vector of objects

I have a simple structure in my code to read the values from a txt file.我的代码中有一个简单的结构来读取 txt 文件中的值。 this struct is这个结构是

struct read_data_into_file{
std::string Fname,Lname;
double score[5];
double sum;
};

then I have a function to open and read from the file.然后我有一个函数来打开和读取文件。

void ReadFromFile(){
std::ifstream f;
read_data_into_file Newobj;
std::vector<read_data_into_file> obj;
f.open("/Users/vibhorsagar/Desktop/cslabs/cs-116lab1/cs-116lab1/input.txt");
if(!f.is_open())
{
    std::cout<<"error opening file";

}
else{
    while(!f.eof())
    {
        f>>Newobj.Fname>>Newobj.Lname;
        for(count=0;count<5;count++)
        {
        f>>Newobj.score[count];
            Newobj.sum+=Newobj.score[count];
        }
        obj.push_back(Newobj);
    }
        for(int i=0;i<obj.size();i++){
        std::cout<<obj[i]<<" "<<std::endl;

    }
}
f.close();

I am kinda confused on how to read the data in the vector.我对如何读取向量中的数据有点困惑。 The text file contains a bunch of names and numbers.文本文件包含一堆名称和数字。

Andria Senger 80 65 81 76安德里亚·森格 80 65 81 76

Nathalie Witherspoon 96 99 93娜塔莉威瑟斯彭 96 99 93

Maribel Danner 94 53 96 91 60玛丽贝尔丹纳 94 53 96 91 60

Kara Hogan 52 75 93 97 95卡拉霍根 52 75 93 97 95

Elliot Kremer 74 50 96 68埃利奥特·克雷默 74 50 96 68

Keena Scheurer 100 90 57 97 90基娜·舒勒 100 90 57 97 90

Sindy Morfin 74 67辛迪莫芬 74 67

Janeth Saito 81 60 60珍妮丝斋藤 81 60 60

These are the contents of the txt file.这些是txt文件的内容。 I was thinking I could store individual name as a first name and a last name and the numbers after that into an array.我想我可以将个人姓名作为名字和姓氏以及之后的数字存储到数组中。 and then find their individual average.然后找到他们的个人平均值。 but when I try to print the vector in the function I get an error但是当我尝试在函数中打印向量时出现错误

Invalid operands to binary expression ('std::__1::ostream' (aka 'basic_ostream') and 'std::__1::__vector_base >::value_type' (aka 'read_data_into_file'))二进制表达式的无效操作数('std::__1::ostream'(又名'basic_ostream')和'std::__1::__vector_base >::value_type'(又名'read_data_into_file'))

Also the numbers in each line are not 5, but I am using that for the array size because that was the highest number in a line.每行中的数字也不是 5,但我将其用于数组大小,因为这是一行中的最大数字。 I don't know how to dynamically set the array size for each line.我不知道如何为每一行动态设置数组大小。 I am using Xcode on Mac to write and compile.我在 Mac 上使用 Xcode 来编写和编译。

if there is any other way to do this, I can use the help or if you can suggest a better way to do this.如果有任何其他方法可以做到这一点,我可以使用帮助,或者如果您可以提出更好的方法来做到这一点。 Thank You!谢谢你!

There are some things to consider with your problem.您的问题需要考虑一些事项。 First, you must account for the different amount of scores in each file entry.首先,您必须考虑每个文件条目中不同数量的分数。 Also, you have to deal with empty lines in the file.此外,您必须处理文件中的空行。

The code below will make it work for your problem.下面的代码将使它适用于您的问题。

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>

void ReadFromFile(){
    std::vector<read_data_into_file> obj;

    std::ifstream inFile;
    inFile.open("input.txt");

    while(!inFile.eof())
    {
        std::string curLine;
        std::getline(inFile, curLine);

        if (!curLine.size())
            continue;

        std::stringstream curStr(curLine);

        read_data_into_file newObj;
        curStr >> newObj.Fname;
        curStr >> newObj.Lname;

        int i = 0;
        while (curStr.good()) {
            curStr >> newObj.score[i++];
        }

        newObj.sum = 0;
        while (i--) {
            newObj.sum += newObj.score[i];
        }

        obj.push_back(newObj);
    }

    inFile.close();

    for (auto& p : obj) {
        std::cout << p.Fname << " " << p.Lname << " " << p.sum << std::endl;
    }
}

However, a proper solution would be to use an std::vector<double> score;但是,正确的解决方案是使用std::vector<double> score; instead of double score[5];而不是double score[5]; in your read_data_into_file structure.在您的read_data_into_file结构中。 Then your code will be:那么你的代码将是:

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>

struct read_data_into_file{
std::string Fname,Lname;
std::vector<double> score;
double sum;
};

void ReadFromFile(){
    std::vector<read_data_into_file> obj;

    std::ifstream inFile;
    inFile.open("input.txt");

    while(!inFile.eof())
    {
        std::string curLine;
        std::getline(inFile, curLine);

        if (!curLine.size())
            continue;

        std::stringstream curStr(curLine);

        read_data_into_file newObj;
        curStr >> newObj.Fname;
        curStr >> newObj.Lname;

        while (curStr.good()) {
            double curScore;
            curStr >> curScore;
            newObj.score.push_back(curScore);
        }

        newObj.sum = 0;
        for (auto& s : newObj.score)
            newObj.sum += s;

        obj.push_back(newObj);
    }

    inFile.close();

    for (auto& p : obj) {
        std::cout << p.Fname << " " << p.Lname << " " << p.sum << std::endl;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从文件中读取数字并在数组中使用它们? - How can I read numbers from an file and use them in an array? 如何从文本文件中读取整数并将其存储在数组中? - How to read integers from a text file and store them in an array? 如何从文件中读取并存储在 c++ 中的对象数组中 - How to read from file and store in array of objects in c++ 如何从文件中读取数据,然后将数据转换为 int 并将它们存储在向量中 - How to read data from a file then convert the data to an int and store them in a vector 你如何从输入文件中读取字符并将它们存储到向量中? - How do you read chars from an input file and store them into a vector? 如何从.csv文件中读取值并将其存储在向量中? - c++ How can I read values from a .csv file and store them in a vector? 如何从文件读入类对象的向量? - How to read from file into a vector of class objects? 如何从具有字符串和整数的文件中获取整个整数并将它们存储到 C++ 中的数组中? - How to obtain the whole integers from file has strings and integers and store them into array in C++? 从文本文件中读取整数并将其存储到数组中 - Read integers from text file and store them into an array 如何从文件中读取前256位以将它们存储在两个数组中 - How to read first 256 bits from a file to store them in two arrays
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM