从文件中读取所有数据
Posted
技术标签:
【中文标题】从文件中读取所有数据【英文标题】:Reading all data from a file 【发布时间】:2019-05-12 21:46:59 【问题描述】:我正在编写一个程序来显示来自文本文件的学生成绩报告。我在正确列出学生的课程时遇到问题,因为第一门课程被跳过并替换为文件下一行的第一个字符串。我也只能显示文件中一个学生的结果,而不是所有学生的结果。
我不确定如何解决数据中跳过的课程。我尝试了几种不同的方式调整开始循环,但我要么得到我目前拥有的结果,要么得到一个只有一个学生的连续循环
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
string ID, lastName, firstName;
char midInt, sex, letterGrade;
string phoneNum, classLevel, courseName;
int regCourses, courseCredit;
double numGrade, totalNumGrade;
int credits2018, credits2019;
double cumGPA2018, cumGPA2019;
int i;
ofstream out_stream;
ifstream in_stream;
in_stream.open("C:/Users/mss3s_000/Desktop/Stud.txt");
if (in_stream.fail())
cout << "Input file opening fail.\n";
exit(1);
if (!in_stream.eof()) //I think the issue with not reading all the data is with this loop, but I'm not sure.
while (in_stream >> ID >> lastName >> firstName >> midInt >> phoneNum >> sex >> classLevel >> credits2018 >> cumGPA2018 >> regCourses >> courseName >> courseCredit >> letterGrade)
switch (sex)
case 'M':
/*sex = "Male";*/
break;
case 'F':
/*sex = "Female";*/
break;
if (classLevel == "FR")
classLevel = "Freshman";
else if (classLevel == "SO")
classLevel = "Sophomore";
else if (classLevel == "JR")
classLevel = "Junior";
else if (classLevel == "SR")
classLevel = "Senior";
if(letterGrade == 'A')
numGrade = 4.0;
else if(letterGrade == 'B')
numGrade = 3.0;
else if(letterGrade == 'C')
numGrade = 2.0;
else if(letterGrade == 'D')
numGrade = 1.0;
else if(letterGrade == 'F')
numGrade = 0.0;
cout << "**************************************************" << endl;
cout << "Student ID number: " << ID << endl;
cout << "Name: " << lastName << ", " << firstName << " " << midInt << endl;
cout << "Tel: " << phoneNum << endl;
cout << "Gender: " << sex << endl;
cout << "Class Level: " << classLevel << endl;
if (regCourses == 0)
cout << "Registration of Spring 2019: " << "No" << endl;
cout << "**************************************************" << endl;
cout << "Registration of Spring 2019: " << "Yes" << endl;
cout << "\n Unofficial Report Card" << endl;
cout << "COURSE CREDITS GRADE" << endl;
cout << "====== ======= ======" << endl;
credits2019 = 0;
cumGPA2019 = 0.0;
for (i = 1; i <= regCourses; i++) //I thought this loop would show all the courses but it is skipping the first course
in_stream >> courseName;
in_stream >> courseCredit;
in_stream >> letterGrade;
if (letterGrade != 'W')
credits2019 += courseCredit;
totalNumGrade += (courseCredit * numGrade);
cumGPA2019 = totalNumGrade / credits2019;
cout << courseName << " " << courseCredit << " " << letterGrade << endl;
out_stream << courseName << " " << courseCredit << " " << letterGrade << endl;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << "\nCredits for Spring 2019: " << credits2019 << endl;
cout << "GPA till Fall 2018: " << cumGPA2018 << endl;
cout << "Total Credits: " << (credits2018 + credits2019) << endl;
cout << "New Cumulative GPA: " << ((credits2019 * cumGPA2019) + (credits2018*cumGPA2018)) / (credits2019 + credits2018) << endl;
cout << "**************************************************\n" << endl;
in_stream.close();
return 0;
我希望在课程标题下列出所有课程,并在文件中显示所有学生的信息。
【问题讨论】:
您的数据是什么样的?您是否有机会将其减少很多以仅显示您遇到的问题?阅读minimal reproducible example 以获得一些指示。 如果您的输入是基于行的,则使用std::getline
一次读取一行,然后使用std::istringstream
读取该行的数据。这种问题在这个网站上被问了很多很多次。我相信一些搜索将为您带来解决方案。 Here is one I wrote a while ago
【参考方案1】:
您可以使用getline()
逐行读取文件并检查返回值以了解您是否已到达文件末尾。
我做了一个简单的例子,从文件中读取所有数据并将它们加载到“学生列表”中。
你可以这样做:
数据文件
name:Bob
sex:Male
age:18
name:Alice
sex:female
age:16
C++ 代码
#include <iostream>
#include <fstream>
#include <vector>
struct Student
std::string name;
std::string sex;
std::string age;
void clear()
name.clear();
sex.clear();
age.clear();
;
std::pair <std::string, std::string> breakdown(const std::string & line);
int main()
std::string data_file("../tmp_test/data.txt");
std::vector <Student> students;
std::ifstream in_s(data_file);
if(in_s)
std::string line;
Student current;
while(getline(in_s, line))
std::pair <std::string, std::string> pair = breakdown(line);
if(pair.first == "name") // first field
current.name = pair.second;
else if(pair.first == "sex")
current.age = pair.second;
else if(pair.first == "age") // last field
current.sex = pair.second;
students.push_back(current);
current.clear(); // Not necessary
else
std::cout << "Info: Unknown field encountered !" << std::endl;
in_s.close();
else
std::cout << ("Error: Could not open file: " + data_file) << std::endl;
// Do what you want with the loaded data
for(Student s : students)
std::cout << "Name: " << s.name << "\nSex: " << s.sex << "\nAge: " << s.age << "\n";
std::cout << std::endl;
return 0;
std::pair <std::string, std::string> breakdown(const std::string & line)
bool found(false);
unsigned int i;
for(i = 0; !found && (i < line.length()); ++i)
if(line[i] == ':')
found = true;
--i;
if(found)
return std::make_pair<std::string, std::string>(line.substr(0, i), line.substr(i+1, line.size()-i));
else
return std::make_pair<std::string, std::string>("", "");
输出
姓名:鲍勃 性别:18 年龄:男
姓名:爱丽丝 性别:16 年龄:女性
对我来说效果很好。当然,如果数据文件损坏,我没有处理,...但这不是重点。我试图让它尽可能简单,只是为了展示它是如何完成的。
希望对你有帮助。
【讨论】:
以上是关于从文件中读取所有数据的主要内容,如果未能解决你的问题,请参考以下文章