【问题标题】:Read lines from text file and store into array从文本文件中读取行并存储到数组中
【发布时间】:2021-11-29 04:59:22
【问题描述】:

如何从文本文件中读取行并将它们存储到数组中。例如,我有一个包含 45 行不同行的文本文件。

我的尝试:

int main()
{
    int a[45];
    ifstream myfile("enroll_assg.txt");

    if(!myfile){
        cout << "Error opening file" << endl;
        return -1;
    }

    if(myfile.is_open()){
        while(getline(myfile, a, '\n') == 1){

        }
    }

}

我应该用这个做一个哈希表。我在 getline() 的 while 循环中遇到错误。它是说“没有重载函数的实例”。

我有 45 行,每行都有空格。每一行看起来像:

9650376 乔治琼斯 CS 4.5 ... ...

学生.cpp:

template <typename Comparable>
Student<Comparable>::Student()
{
    // Add your code
    this->fname = fname;
    this->lname = lname;
    this->gpa = gpa;
    this->department = department;
    this->id = id;
    this->bucketId = bucketId;
}

student.cpp 也有变量的 getter 和 setter。

【问题讨论】:

  • int a[45]; 为 45 个整数变量分配空间。这将如何保存文本行?必读:Why is iostream::eof inside a loop condition (i.e. while (!stream.eof())) considered wrong?
  • @RetiredNinja 我更改了它,现在使用 if(my.is_open()){ 代替。我怎么能这样做我是新手,现在迷路了。
  • 按出现的顺序一次解决一个问题。第一个问题是:int a[45];为 45 个整数变量分配空间。这将如何保存文本行?你解决了吗?
  • 很好奇你从哪里得到std::getline 成功返回 1 的想法。

标签: c++


【解决方案1】:

更好的替代方法是使用std::vector,如下所示。与内置数组相比,使用vector优势在于,由于vector 是一个动态大小的容器,我们不需要事先知道其中有多少学生/条目输入文件。我们可以动态添加这些信息。

下面显示的程序使用struct 来表示给定的Student,它还使用了std::vector。您可以将此程序用作参考(起点)。它从输入文本文件读取学生信息,并将该信息存储在vectorStudent 中。

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
//this class represents a Student
class Student
{
    public:
        std::string firstName, lastName, courseName ;
        unsigned long id = 0;
        float marks = 0;
    
};

int main()
{
   std::ifstream inputFile("input.txt");
   std::string line;
   std::vector<Student> myVec;//create a vector of Student objects
   if(inputFile)
   {
       while(std::getline(inputFile, line))
       {
           Student studentObject;
           std::istringstream ss(line);
           //read the id 
           ss >> studentObject.id;
           
           //read the firstname 
           ss >> studentObject.firstName;
           
           //read the lastname 
           ss >> studentObject.lastName;
           
           //read the courseName
           ss >> studentObject.courseName;
           
           //read the marks 
           ss >> studentObject.marks; 
           
           if(ss)//check if input succeded
           {
               myVec.emplace_back(studentObject);//add the studentObject into the vector
           }
       }
   }
   else 
   {
       std::cout<<"File cannot be opened"<<std::endl;
   }
   
   //lets print out the elements of the vecotr to confirm that all the students were correctly read 
   for(const Student &elem: myVec)
   {
       std::cout << elem.id << ": "<<elem.firstName<<" "<<elem.lastName<<" "<<elem.courseName<<" "<<elem.marks <<std::endl;
   }
    return 0;
}

以上程序的输出可见here

【讨论】:

  • 如果我已经有一个 student.cpp 类,我还需要一个 struct Student 吗? student.cpp 类有一个 student(),它分配 fname、lname、gpa 等,还有 getter 和 setter 方法。
  • @codingisfun543 因为你已经有一个student.cpp,你不需要创建一个单独的结构。但是由于在您的问题中您没有提到任何关于 student.cpp 的内容,我们不知道您是否拥有它。所以我创建了自己的结构。您可以使用自己的 student.cpp (class/struct)
  • 所以我将它从 std::vector myVec 更改为 std::vector myVec,并将其从 student studentObject 更改为 string studentObject。这一切都好吗?我在所有“ss >> ...”上都收到错误。
  • 很抱歉没有正确解释我不想提供一堆不必要的信息。
  • @codingisfun543 你必须使用std::vector&lt;Student&gt;。这是因为我们需要一个 Student 对象的向量,而不是像你所拥有的 int 的向量。在你的 student.cpp 中,你有自己的 structclass 吗?你可以使用我上面给出的结构。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-09
  • 1970-01-01
  • 2023-03-14
  • 2019-07-26
  • 2014-08-04
相关资源
最近更新 更多