结构体数组在读取TXT文件信息时如果要输入文件路径是直接复制粘贴文件的地址吗?
如果是的话为什么我的这个程序还是读取不了文件
如果不是的话,应该输出什么样的路径,能具体说说要怎么操作吗(就是怎么确定一个文件的路径是什么然后输入程序中),对文件读取这一块不了解
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include <cassert>
#include<sstream>
using namespace std;
const int MAX_STUDENT_NUM = 100;
string file_path;
int student_num = 0; // 记录已经读入学生数
// 学生结构体
struct Student {
string id; // 学号
string name; // 姓名
double chinese; // 语文成绩
double math; // 数学成绩
double english; // 英语成绩
double peer_score; // 同学互评分
double morality_score; // 品德成绩
double teacher_score; // 任课教师评分
double average; // 考试成绩
int rank; // 考试名次
double total_score; // 综合测评总分
int total_rank; // 综合测评名次
}studentss[MAX_STUDENT_NUM];
// 按照从大到小的顺序比较学生成绩,用于排序
bool compare_averages(Student a, Student b)
{
if (a.average == b.average)
{
return a.id < b.id;
}
return a.average > b.average;
}
// 按照从大到小的顺序比较学生总分,用于排序
bool compare_total_scores(Student a, Student b)
{
if (a.total_score == b.total_score)
{
return a.id < b.id;
}
return a.total_score > b.total_score;
}
// 读入学生信息
void read_students()
{
ifstream input_file(file_path); // 打开文件
// assert(fin.is_open());
if (!input_file.is_open())
{
cout << "文件打开失败!" << endl;
return;
}
string line;
getline(input_file, line);
while (getline(input_file, line))
{
stringstream ss(line);
ss >> studentss[student_num].id >> studentss[student_num].name >> studentss[student_num].chinese >> studentss[student_num].math >> studentss[student_num].english >> studentss[student_num].peer_score >> studentss[student_num].morality_score >> studentss[student_num].teacher_score;
student_num++;
}
input_file.close();//关闭文件
cout << "成功读入" << student_num << " 名学生的信息!" << endl;
}
// 计算学生考试成绩和名次
void calc_exam_score_and_rank()
{
for (int i = 0; i < student_num; i++)
{
studentss[i].average = (studentss[i].chinese + studentss[i].math + studentss[i].english) / 3.0; // 计算考试成绩
}
sort(studentss, studentss + student_num, compare_averages); // 按照考试成绩排序
// 计算考试名次
for (int i = 0; i < student_num; i++)
{
if (i == 0)
{
studentss[i].rank = 1;
}
else if (studentss[i].average == studentss[i - 1].average)
{
studentss[i].rank = studentss[i - 1].rank;
}
else
{
studentss[i].rank = i + 1;
}
}
}
// 计算学生综合测评总分和名次
void calc_total_score_and_rank()
{
for (int i = 0; i < student_num; i++)
{
studentss[i].total_score = studentss[i].average * 0.6 + studentss[i].peer_score * 0.1
+ studentss[i].morality_score * 0.1 + studentss[i].teacher_score * 0.2; // 计算综合测评总分
}
sort(studentss, studentss + student_num, compare_total_scores); // 按照综合测评总分排序
// 计算综合测评名次
for (int i = 0; i < student_num; i++)
{
if (i == 0)
{
studentss[i].total_rank = 1;
}
else if (studentss[i].total_score == studentss[i - 1].total_score)
{
studentss[i].total_rank = studentss[i - 1].total_rank;
}
else
{
studentss[i].total_rank = i + 1;
}
}
}
// 查询学生信息
void query_student_info()
{
string id;
cout << "请输入要查询的学生学号:";
cin >> id;
bool found = false;
for (int i = 0; i < student_num; i++)
{
if (studentss[i].id == id)
{
cout << "学号:" << studentss[i].id << endl;
cout << "姓名:" << studentss[i].name << endl;
cout << "语文成绩:" << studentss[i].chinese << endl;
cout << "数学成绩:" << studentss[i].math << endl;
cout << "外语成绩:" << studentss[i].english << endl;
cout << "考试平均成绩:" << studentss[i].average << endl;
cout << "考试名次:" << studentss[i].rank << endl;
cout << "同学互评分:" << studentss[i].peer_score << endl;
cout << "品德成绩:" << studentss[i].morality_score << endl;
cout << "任课教师评分:" << studentss[i].teacher_score << endl;
cout << "综合测评总分:" << studentss[i].total_score << endl;
cout << "综合测评名次:" << studentss[i].total_rank << endl;
found = true;
break;
}
}
if (!found)
{
cout << "没有找到对应学生信息!" << endl;
}
}
//修改学生信息
void modify_student_info()
{
string id;
cout << "请输入要修改的学生学号:";
cin >> id;
bool found = false;
for (int i = 0; i < student_num; i++)
{
if (studentss[i].id == id)
{
cout << "请输入新的语文成绩:";
cin >> studentss[i].chinese;
cout << "请输入新的数学成绩:";
cin >> studentss[i].math;
cout << "请输入新的英语成绩:";
cin >> studentss[i].english;
cout << "请输入新的同学互评分:";
cin >> studentss[i].peer_score;
cout << "请输入新的品德成绩:";
cin >> studentss[i].morality_score;
cout << "请输入新的任课教师评分:";
cin >> studentss[i].teacher_score;
// 重新计算该学生的信息
studentss[i].average = (studentss[i].chinese + studentss[i].math + studentss[i].english) / 3.0;
calc_exam_score_and_rank();
calc_total_score_and_rank();
found = true;
break;
}
}
if (!found)
{
cout << "没有找到对应学生信息!" << endl;
}
}
void delete_student_info()
{
string id;
cout << "请输入要删除的学生学号:" << endl;
cin >> id;
for (int i = 0; i < student_num; i++)
{
if (studentss[i].id == id)
{
for (int j = i; j < student_num - 1; j++)
{
studentss[j] = studentss[j + 1];
}
student_num--;
cout << "学生信息删除成功!" << endl;
return;
}
}
cout << "没有找到要删除的学生信息!" << endl;
}
void export_student_info()
{
string file_path;
cout << " 请输入要导出学生成绩的文件路径:";
cin >> file_path;
ofstream output_file(file_path);
if (!output_file.is_open())
{
cout << "无法打开文件!" << endl;
return;
}
output_file.close();
cout << "学生成绩已成功导出到文件" << file_path << endl;
}
void bSystem()
{
cout << "进入学生数据信息处理系统" << endl;
/* step 1 读取学生信息*/
read_students();
calc_exam_score_and_rank();
calc_total_score_and_rank();
int selectA = 0;
while (selectA != 5)
{
cout << endl;
/* step 2 */
cout << "1. 查询数据 " << endl;
cout << "2. 修改数据" << endl;
cout << "3. 删除数据" << endl;
cout << "4. 导出学生成绩" << endl;
cout << "5. 退出子系统" << endl;
cout << endl << endl;// 输入
cin >> selectA;
cout << "您输入的是 " << selectA << endl;
if (selectA == 1)
{
query_student_info();
}
else if (selectA == 2)
{
modify_student_info();
}
else if (selectA == 3)
{
delete_student_info();
}
else if (selectA == 4)
{
export_student_info();
}
else if (selectA == 5)
{
break;
}
else
{
cout << "错误的输入,请重新输入!" << endl;
}
}
}
int main()
{
cout << "请输入学生信息文件的路径:";
cin >> file_path;
bSystem();
return 0;
}