从文件中读取所有数据
Reading all data from a file
我正在编写一个程序,以显示文本文件中学生的成绩报告。我在正确列出学生的课程时遇到了问题,因为第一门课程是跳过并用文件中下一行的第一条字符串替换的。我也只能为文件中的一个学生而不是所有学生显示结果。
我不确定如何解决数据中的跳过课程。我已经尝试调整几种不同的方法,但是我要么得到当前的结果,要么只有一个学生的连续循环
#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;
}
我希望在课程标题下列出所有课程,并显示文件中所有学生的信息。
您可以使用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>("", "");
}
Outpout
名称:鲍勃
性:18
年龄:男性名称:爱丽丝
性:16
年龄:女性
它对我效果很好。
当然,如果数据文件损坏,我没有处理……但这不是这里的重点。我试图使它尽可能简单,以显示如何完成。
我希望它能有所帮助。
相关文章:
- C++将文本文件中的数据读取到结构数组中
- 为 Sql 服务器实现 odbc 包装器.将数据库数据读取为字符或要求驱动程序将数据转换为 C 类型
- 如何将来自文本文件的数据读取到结构的int阵列中
- 如何将不同长度的数据读取到 2D 数组中
- 从TXT文件到C 的结构向量中的数据读取
- 将sqlite原始数据读取到qbyTearray
- 如何将来自输入TXT文件的数据读取到2 C 数组中
- 使用MQQueue将数据读取到缓冲区中时麻烦
- 在 VC++ 中将类数据读取和写入二进制文件
- 将数据从数据读取器终端获取到GUI(QtCreator)
- C 将数据读取到一个结构,但它跳过了我的一个获取线,使该元素空白
- C++将二进制数据读取到结构
- c++我需要将文件中的数据读取到多维数组中,然后用一种数据类型对数组进行排序.怎样
- 对齐数据:速度取决于恒定数据读取的相对采样大小和采样频率
- 如何将文件中的数据读取到数组中
- 如何使用Visual C++应用程序的完整应用程序数据读取Minidump的应用程序数据
- 如何将csv文件数据读取到数组中
- 用C++将格式化的数值数据读取到数组中
- 正在从文件中将数据读取到结构数组中
- 将分离的数据读取到数组结构c++中