csv 如何合并文件和类类型向量?cpp

fdbelqdn  于 5个月前  发布在  其他
关注(0)|答案(1)|浏览(71)

创建类Student h,它将具有以下特征:
·AM:(int)
●名称:(char *)
● Semester:(int)
·Active?:(bool)
编写一个函数,创建并返回Students类的对象集合,这些对象包含从csv文件读取的数据。文件名作为函数的参数。
写一个函数,将打印AM和每个活跃的学生的名字,在她的集合前一个函数。写一个主函数,将显示上述操作。学生类将没有公共属性,只有必要的方法来实现上述要求

#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

class Student {
private:
    int AM;
    string Name;
    int Semester;
    bool Active;

public:
    // Constructor
    Student(int am,string name, int semester, bool active)
        {
          AM = am;
          Name = name;
          Semester = semester;
          Active = active;
        }
    Student()
        {
          AM = 0;
          Name = "";
          Semester = 0;
          Active = false;
        }
 
    // Getter for AM
    int getAM() {
        return AM;
    }

    // Getter for Name
   string getName() {
        return Name;
    }

    // Getter for Semester
    int getSemester() {
        return Semester;
    }

    // Getter for Active
    bool isActive() {
        return Active;
    }
};

// Function to read students from a CSV file
vector<Student> readStudentsFromFile(string filename) {
    vector<Student> students;

    // ifstream file object is used to access the file
   ifstream file;

   file.open(filename);

    // If the file fails to open for some reason (e.g. the file doesn't exist),
    // the fail member function of the ifstream file object will return true.
    // If it does, we exit the function with an empty vector.
    if (file.fail()) {
        // Output an error message
        cout << "File failed to open." << endl;
        return students;
    }

    Student line;

  while (!file.eof())
  {
   
    getline(file, line);

    students.push_back(line);
  }

  // Close the file as we are now done working with it
  file.close();

    return students;
}

// Function to print active students
void printActiveStudents(vector<Student> students) {
    for (int i = 0; i < students.size(); ++i) {
        if (students[i].isActive()) {
            cout << "AM: " << students[i].getAM() << ", Name: " << students[i].getName() << endl;
        }
    }
}

int main() {
    // Create a collection of Student objects by reading from a CSV file
   vector<Student> studentCollection = readStudentsFromFile("cs.csv");

    // Print AM and Name of each active student
    printActiveStudents(studentCollection);

    return 0;
}

字符串
我不能让这个工作,我需要从一个文件中获取行,并做我被要求做的事情。在我的代码中的问题,它说“消息”:“没有重载函数“getline”的示例匹配参数列表”。我不知道如何得到这个固定我坚持了几个小时,并尝试了不同的东西周围的代码。

rpppsulh

rpppsulh1#

while (!file.eof())

字符串
This is always a bug,在C和C中。
您的C
教科书或教师一定已经讨论过从文件中阅读时处理和检测文件结束条件的正确方法。文件流的文件结束条件在尝试从文件中读取失败后 * 设置。此代码尝试在尝试从文件中读取下一条记录之前 * 检查文件结束条件。如您所知,while循环的条件是在执行循环内容之前 * 检查的。但这只是问题的开始,大部分问题都在这里:

getline(file, line);


std::getline的第二个参数是一个std::string。仅仅因为这个变量的名字是“line”并不会自动将它转换为std::string,C++并不这样工作。你在这里将这个line声明为一个Student对象(默认构造):

Student line;


这就是这一行的作用,它声明了一个Student,名为line。所以它是一个Studentstd::getline不知道任何关于Student对象的信息,它只知道如何将一行文本读入std::string
因此,您别无选择,只能将每一行读入std::string,首先:

std::string line;

while (!std::getline(file, line).eof())


这解决了两个问题:现在每一行都会被读入std::string,并且eof()会在***尝试从文件中读取后***被检查,因此如果失败,循环就会终止,正如预期的那样。
现在,您有了一个std::string,包含了整行代码。您将文件的内容描述为包含逗号分隔的值。
你必须在自己的代码中实现提取和解析一行逗号分隔值的逻辑。C库中没有任何东西可以为你做这件事。从你的问题中,你不清楚你的教科书或C老师是否希望你使用某些特定的CSV解析库,或者你的家庭作业的全部目的就是自己实现CSV解析逻辑,使用迭代器和/或C库中的基本算法(这可能是可能的情况)。
但不管是什么情况,只有你自己知道(在Stackoverflow上,我们不知道你的作业的全部细节),你需要根据你已经读过的或你的C
教科书或老师教过的内容继续下去,并适当地提取Student的四个必需值,一旦提取出来,就把它们传递给Student的构造函数:

Student(int am,string name, int semester, bool active)


一旦实现了适当的逻辑,就可以使用这个构造函数来创建Student对象,并对它执行任何需要执行的操作。

相关问题