【发布时间】:2018-09-06 02:59:21
【问题描述】:
我目前正在学习解析一个包含学生成绩数据的文件。
std::ifstream f;
int main()
{
//std::ifstream f;
parse_store(f, "student_grades.txt"); //this line is throwing the error
//keep the window from shutting down
system("pause");
return 0;
}
parse_store 函数解析 txt 文件,分割每一行数据,然后将tokenized 向量(看起来像这样 ["Hello", "World"])发送到一个类中,该类存储在一个数组中类对象。
上下文:
//parses the file returning a vector of student objects
std::vector<Student> parse_store(std::ifstream file, std::string file_name) {
//array of objects
std::vector<Student> student_info;
//create string to store the raw lines
std::string line;
// the delimiter
std::string delimiter = " ";
//open te file
file.open(file_name);
//create vector to hold the tokenized list
std::vector<std::string> tokenized;
//index
int index = 0;
while (file) {
//keep track of the index
index++;
//create a vector to hold each student's grades (will hold the objects)
std::vector<std::vector<std::string>> grades = {};
//read a line from file
std::getline(file, line);
//delimit the line and send it to the constructor
tokenized = delimitMain(line, delimiter);
student_info.push_back(Student(tokenized, index));
}
file.close();
return student_info;
}
为什么上面的行会抛出错误?我将文件对象放入向量然后返回它的方式有什么问题吗?
【问题讨论】:
标签: c++ file oop c++11 visual-c++