首页 > 编程语言 >C/C++ 学生管理系统的文件读取与写入

C/C++ 学生管理系统的文件读取与写入

时间:2024-09-12 21:25:43浏览次数:1  
标签:读取 int 写入 C++ stu score Student ID cout

代码中文件读取函数read_file()存在一些问题,望指出.

`#include

include

define SIZE 100

using namespace std;

static int nums_stu = 0;

typedef struct Student {
char* name;
char* ID; //占10个字符
double score_one;
double score_two;
double score_three;
double total_scores;
}Student;

Student Create_stu(); //创建学生信息结构体
void Input_student_score(Student* stu); //输入学生信息
void Output_student_score(Student stu); //输出所有学生信息
void select_student(char* ID, Student* stu, int size); //根据ID查找学生信息
void update_student_score(char* ID, int size, double score_one, double score_two, //更新学生信息
double score_three, Student* stu);
void delete_student_score(char* ID, int size, Student* stu); //删除学生信息
int read_file(Student* stu); //读取文件
void write_file(Student* stu); //写入文件

int main()
{
Student stu[SIZE];
Student stu2[SIZE];

for (int i = 0; i < 2; i++) {
	/*stu[i] = Create_stu();*/
	stu2[i] = Create_stu();
	/*Input_student_score(&stu[i]);*/
}
/*write_file(stu);*/
int nums = read_file(stu2);
for (int i = 0; i < nums; i++)
{
	Output_student_score(stu2[i]);
}

/*cout << nums_stu << endl;
while (true) {
	cout << "----------------------------------------------------------------" << endl;
	cout << "输入: 1.查找学生信息 2.更新学生信息 3.删除学生信息	4.输出所有学生信息 5.退出系统" << endl;
	cout << "-" << endl;
	int choice;
	cin >> choice;
	char ID[10];
	switch (choice) {
	case 1:
		cout << "请输入要查找的学生ID:" << endl;
		cin >> ID;
		select_student(ID, stu, nums_stu);
		break;
	case 2:

		cout << "请输入要更新的学生ID:" << endl;
		cin >> ID;
		double score_one, score_two, score_three;
		cout << "请输入新的数学成绩:" << endl;
		cin >> score_one;
		cout << "请输入新的英语成绩:" << endl;
		cin >> score_two;
		cout << "请输入新的计算机成绩:" << endl;
		cin >> score_three;
		update_student_score(ID, nums_stu, score_one, score_two, score_three, stu);
		break;

	case 3:

		cout << "请输入要删除的学生ID:" << endl;
		cin >> ID;
		delete_student_score(ID, nums_stu, stu);
		break;

	case 4:
		for (int i = 0; i < nums_stu; i++) {
			if (stu[i].ID != NULL) {
				Output_student_score(stu[i]);
			}
		}
		break;

	case 5:
		return 0;

	default:
		cout << "输入错误(或不符合规则),请重新输入!" << endl;
		break;
	}
}*/

return 0;

}

Student Create_stu() //创建学生信息结构体
{
Student* stu = new Student;
stu->name = new char[20];
stu->ID = new char[10];
stu->score_one = 0.0;
stu->score_two = 0.0;
stu->score_three = 0.0;
stu->total_scores = 0.0;
return *stu;
}

void Input_student_score(Student* stu) //输入学生信息
{

cout << "Please input the name of student :" << ":" << endl;
cin >> stu->name;
cout << "Please input the ID of student :" << endl;;
cin >> stu->ID;
cout << "Please input the math scores of student :" << endl;
cin >> stu->score_one;
cout << "Please input the english scores of student :" << endl;
cin >> stu->score_two;
cout << "Please input the computer scores of student :" << endl;
cin >> stu->score_three;
cout << endl << endl;
stu->total_scores = stu->score_one + stu->score_two + stu->score_three;
nums_stu++;

}

void Output_student_score(Student stu) //输出学生信息
{
cout << "Name:" << stu.name << endl;
cout << "ID:" << stu.ID << endl;
cout << "Math Scores:" << stu.score_one << endl;
cout << "English Scores:" << stu.score_two << endl;
cout << "Computer Scores:" << stu.score_three << endl;
cout << "Total Scores:" << stu.total_scores << endl;
}

void select_student(char* ID, Student* stu, int size) //根据ID查找学生信息
{
for (int i = 0; i < size; i++) {
if (!strcmp(stu[i].ID, ID)) {
Output_student_score(stu[i]);
return;
}
}
}

void update_student_score(char* ID, int size, double score_one, double score_two, //更新学生信息
double score_three, Student* stu)
{
for (int i = 0; i < size; i++) {
if (!strcmp(stu[i].ID, ID)) {
stu[i].score_one = score_one;
stu[i].score_two = score_two;
stu[i].score_three = score_three;
stu[i].total_scores = score_one + score_two + score_three;
return;
}
}
}

void delete_student_score(char* ID, int size, Student* stu) //删除学生信息
{
for (int i = 0; i < size; i++) {
if (!strcmp(stu[i].ID, ID)) {
stu[i].ID = NULL;
stu[i].score_one = 0;
stu[i].score_two = 0;
stu[i].score_three = 0;
stu[i].total_scores = 0;
nums_stu--;
delete[] stu[i].name;
delete[] stu[i].ID;
cout << "删除成功!" << endl;
return; //删除后,将该位置的ID置为NULL
}
}
}

int read_file(Student* stu)
{
FILE* fp;
int nums = 0;
fopen_s(&fp, "D:\OneDrive\桌面\123.txt", "r");
if (fp == NULL) {
cout << "文件打开失败!" << endl;
return -1;
}
while (fscanf_s(fp, "%18s %9s %lf %lf %lf %lf\n", stu->name, sizeof(stu->name), stu->ID, sizeof(stu->ID),
&stu->score_one, &stu->score_two,&stu->score_three, &stu->total_scores) != EOF)
{
nums++;
stu++;
}
cout << "读取成功!" << endl;
fclose(fp);
return nums;
}

void write_file(Student* stu)
{
FILE* fp;
fopen_s(&fp, "D:\OneDrive\桌面\123.txt", "w");
if (fp == NULL) {
cout << "文件打开失败!" << endl;
return;
}
for (int i = 0; i < nums_stu; i++) {
if (stu[i].ID != NULL) {
fprintf_s(fp, "%s %s %lf %lf %lf %lf\n", stu[i].name, stu[i].ID, stu[i].score_one, stu[i].score_two,
stu[i].score_three, stu[i].total_scores);
}
}
cout << "写入成功!" << endl;
fclose(fp);
}`

标签:读取,int,写入,C++,stu,score,Student,ID,cout
From: https://www.cnblogs.com/solgud-26/p/18411124

相关文章

  • C++中的基本运算符----逻辑运算符(&&、||、!)的实例讲解
    在C++中,逻辑运算符用于处理布尔值(true和false),并用于复合条件的判断。主要的逻辑运算符包括:目录1.逻辑与运算符(&&):2.逻辑或运算符(||):3.逻辑非运算符(!):1.逻辑与运算符(&&):当且仅当两个操作数都为true时,结果才为true。示例:if(a>0&&b>0)下面......
  • C++插件管理系统
    插件加载目录结构execute   plug.exe   plugify.dll   plugify.pconfig   res      cpp-lang-module.pmodule      example_plugin.pplugin      bin         cpp-lang-module.dll         exampl......
  • C++---string类常见接口
    介绍 string类详情===>>>https://cplusplus.com/reference/string/string/?kw=string1.string是表示字符串的字符串类(感觉就像一个动态的字符数组)2.该类的接口与常规容器的接口基本相同,再添加了一些专门用来操作string的常规操作。3.string在底层实际是:basic_string模......
  • C++入门基础知识64——【关于 C+++数据抽象】
    成长路上不孤单......
  • C++音视频毕设项目计划书
    最近我经常收到私信,询问如何准备秋季招聘,以及学生阶段如何参与实习或进行小项目来提升自己的技能。我个人建议,找到相关行业的公司进行实习是个不错的选择。不过,自己也可以尝试做一些小项目,不断改进和优化,以提升动手能力。以下是一个音视频项目计划书,分享给大家:项目开发计划书:流媒......
  • C++读取命令行参数的学习(BOOST库)
    在c++工程中,经常需要通过命令行参数来获取程序运行所需要的信息。作者在实际工作中学习了Boost库,这里根据作者的理解,写了一个依托boost库完成命令行参数提取的程序模版,请大佬批评!!#defineOK0#defineExit-99//主程序#include<iostream>#include"XApp.h"intmain(int......
  • 题解 力扣 LeetCode 105 从前序与中序遍历序列构造二叉树 C/C++
    题目传送门:105.从前序与中序遍历序列构造二叉树-力扣(LeetCode)https://leetcode.cn/problems/construct-binary-tree-from-preorder-and-inorder-traversal/description/每次在中序遍历序列片段中找到前序遍历序列队首,这是该层的根节点该位置左侧是左子树,右侧是右子树再......
  • C++面试题整理 1
    1.new和malloc什么区别?new和malloc都用于在堆上分配内存,new是c++中的关键字,分配内存后还会调用构造函数2.std中unorded_map,map,multimap有什么区别?unorderd_map中元素不按键值排序,底层数据结构是哈希表,相对map查询速度快,内存开销大map中元素按键值排序,底层数据结构是红黑......
  • Python文件操作:文件的读取和写入(文本文件、二进制文件)①
    文章目录1.文件操作基础1.1打开文件1.2关闭文件2.文本文件操作2.1读取文本文件2.1.1逐行读取2.1.2读取所有内容2.1.3读取所有行2.2写入文本文件2.2.1写入内容2.2.2追加内容3.二进制文件操作3.1读取二进制文件3.2写入二进制文件4.综合示例4.1示例描......
  • cloud studio配置C++环境
    cloudstudio腾讯推出的云IDE,里面有很多现成的语言环境,这里讲一下C++的环境配置1.选择C++环境模板创建就可以了2.可以直接run或者g++编译3.安装插件第一个C++插件需要自己离线下载上传安装上去,在cloudstudio的插件商店里面搜索不到自行搜索怎么下载离线插件4task和laun......