C++ 文件操作(简易的学籍管理系统)
这是一个简易的学籍管理系统,大一时居然三个人写了一千多行......年少无知啊!欢迎摘果实! 1 #include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std; /*
*信息结构
*/
/*struct stu
{
string id;
string name;
string gender;
int age;
string magor;
string prize;
};*/ bool isFind = false;//是否找到的标识 void find(const char *file, const int id);
void del(const char *file, const int id);
void change(const char *file, const int id); void add(const char *file)
{
ofstream out;
out.open(file, ios::app);
if (!out.is_open())
{
cout << "文件打开失败" << endl;
return;
}
else
{
int id, age;
string name, major, prize, gender;
cout << "输入学号、姓名、性别、年龄、专业、奖项:" << endl;
cin >> id >> name >> gender >> age >> major >> prize;
find(file, id);
if(!isFind)
out << id << "\n" << name << "\n" << gender << "\n" << age << "\n" << major << "\n" << prize << "\n";
else
{
cout << "该学号已存在..." << endl;
return;
}
cout << "添加成功" << endl;
out.close();
}
} void find(const char *file, const int id)
{
ifstream in;
in.open(file, ios::in | ios::binary);
if (!in.is_open())
{
cout << "打开文件错误" << endl;
return ;
}
while(in.peek() != EOF)
{
int temp;
stringstream s;
string line;
getline(in, line);//读字符串
s << line;
s >> temp;
if (temp == id)
{
isFind = true;//找到
int age;
string name, major, prize, gender;
cout << "找到记录:"<< endl;
getline(in, line);//读名字
s << line; s >> name;
getline(in, line);//读性别
s << line; s >> gender;
getline(in, line);//读年龄
s << line; s >> age;
getline(in, line);//读专业
s << line; s >> major;
getline(in, line);//读奖项
s << line; s >> prize; cout << "学号:" << temp << " ";
cout << "姓名:" << name << " ";
cout << "性别:" << gender << " ";
cout << "年龄:" << age << " ";
cout << "专业:" << major << endl;
cout << "奖项:" << prize << endl;
}
}
in.close();
} void del(const char *file, const int id)
{
isFind = false;
find(file, id);//找到要删的位置
if(isFind)
cout << "正在删除..." << endl;
else
{
cout << "无此纪录!" << endl;
isFind = false;
return;
}
ifstream in;
in.open(file, ios::in | ios::binary);
if (!in.is_open())
{
cout << "打开文件错误" << endl;
return;
}
string tempStr;
while (in.peek() != EOF)
{
int temp;
string line;
getline(in, line);//读字符串
stringstream s;
s << line;
s >> temp;
if (temp == id)
{
int delLine = ;
while (delLine--)
getline(in, line);
}
else
{
//getline(in, line);//读字符串
tempStr += line;
tempStr += "\n";
} }
in.close();
//重新写入文件
ofstream out;
out.open(file, ios::out);
if (!out.is_open())
{
cout << "打开文件错误,删除失败!" << endl;
return;
}
else
{
out << tempStr;//重新写入
cout << "删除完成!" << endl;
}
out.close();
} void change(const char *file, const int id)
{
isFind = false;
find(file, id);//找到要改的目标
if (isFind)
{
int age;
string name, major, prize, gender;
cout << "输入新的姓名、性别、年龄、专业、奖项:" << endl;
cin >> name >> gender >> age >> major >> prize;
ifstream in;
in.open(file, ios::in | ios::binary);
if (!in.is_open())
{
cout << "文件打开失败!" << endl;
return;
}
string tempStr;
while (in.peek() != EOF)
{
int temp;
string line;
stringstream s;
getline(in, line);
s << line;
s >> temp;
if (temp == id)
{
tempStr += to_string(id) + "\n";
tempStr += name + "\n";
tempStr += gender + "\n";
tempStr += to_string(age) + "\n";
tempStr += major + "\n";
tempStr += prize + "\n";//加入新信息
int delLine = ;
while (delLine--)
getline(in, line);//跳过旧信息
}
else
{
tempStr += line;
tempStr += "\n";
}
}
in.close();//别忘记 //重新写入文件
ofstream out;
out.open(file, ios::out);
if (!out.is_open())
{
cout << "打开文件错误,删除失败!" << endl;
return;
}
else
{
out << tempStr;//重新写入
cout << "修改完成!" << endl;
}
out.close();
}
} int main()
{
const char *file = "D:\\homework\\DB\\test.txt";//文件地址
while (true)
{
int ans;
cout << "--------------------------------" << endl;
cout << "--> 1.插入信息 <--" << endl;
cout << "--> 2.查找信息 <--" << endl;
cout << "--> 3.删除信息 <--" << endl;
cout << "--> 4.修改信息 <--" << endl;
cout << "--> 0.退出Demo <--" << endl;
cout << "--------------------------------" << endl;
cout << "输入指令~$ ";
cin >> ans;
switch (ans)
{
case :
{
cout << "已退出!" << endl;
return ;
}
break;
case :
{
add(file);
}
break;
case :
{
cout << "输入要查找的学号:";
int id;
cin >> id;
find(file, id);
}
break;
case :
{
cout << "输入要删除的学生学号:";
int id;
cin >> id;
del(file, id);
}
break;
case :
{
cout << "输入要修改的学生学号:";
int id;
cin >> id;
change(file, id);
}
default:
{
cout << "输入有误!" << endl;
}
break;
}
} return ;
}
C++ 文件操作(简易的学籍管理系统)的更多相关文章
- 基于C语言文件操作的学生成绩管理系统
原理 在一个班级学生成绩管理系统中,希望处理每个学生的学习情况信息,其中包括学生的学号.姓名.各科名称和成绩等并能使管理人员通过界面完成对学生信息的录入及对学生信息的录入及对数据的查找.浏览.插入.排 ...
- Qt实现学生学籍管理系统(文件存储)
记录 19:53 2019-07-30 在小学期学c++做完课设后萌生了把写完的课设放在博客上的想法,于是,我第一篇博客诞生了. 22:32:19 2019-07-30 下棋 16:04:56 201 ...
- java课堂测试样卷-----简易学籍管理系统
程序设计思路:分别建立两个类:ScoreInformation类(用来定义学生的基本信息以及设置set和get函数)ScoreManagement类(用来定义实现学生考试成绩录入,考试成绩修改,绩点计 ...
- 员工管理系统+字符编码+Python代码文件操作
员工管理系统+字符编码+Python代码文件操作 1.员工管理系统 1.1 debug 代码调试 1.先使用鼠标左键在需要调试的代码左边点击一下(会出现一个红点)2.之后右键点击debug运行代码 ...
- android 文件操作类简易总结
android 文件操作类(参考链接) http://www.cnblogs.com/menlsh/archive/2013/04/02/2997084.html package com.androi ...
- c++大作业--学籍管理系统--
1.题目描写叙述 学籍管理系统: 依据信息管理系统的业务流程.要求以及所要实现的目标,完毕下面功能: (1)建立学生档案的管理和维护.实现计算机自己主动化管理体制. (2)建立学生成绩管理机制,在计算 ...
- 【C语言期末实训】学生学籍管理系统
目录: 一,设计要求 ,总体要求: ,具体功能: 二,设计框架 三,程序代码 ,声明函数和头文件 ,声明结构体 ,声明全局变量 ,主体启动函数 ,主菜单函数 ,创建学生档案函数 ,编辑学生档案函数 , ...
- C++实现控制台学生学籍管理系统
操作流程 创建文件 创建管理类 管理类负责的内容如下: 提供与用户的沟通菜单界面 实现对职工增删改查的操作 数组数据与文件的读写交互 菜单功能实现 在StudentManager.h中定义Show ...
- Django 小实例S1 简易学生选课管理系统 11 学生课程业务实现
Django 小实例S1 简易学生选课管理系统 第11节--学生课程业务实现 点击查看教程总目录 作者自我介绍:b站小UP主,时常直播编程+红警三,python1对1辅导老师. 课程模块中,学生需要拥 ...
随机推荐
- [Swift]LeetCode167. 两数之和 II - 输入有序数组 | Two Sum II - Input array is sorted
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- [Swift]LeetCode354. 俄罗斯套娃信封问题 | Russian Doll Envelopes
You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envel ...
- [Swift]LeetCode557. 反转字符串中的单词 III | Reverse Words in a String III
Given a string, you need to reverse the order of characters in each word within a sentence while sti ...
- 插入与归并(python)(原创)
根据维基百科的定义: 插入排序是迭代算法,逐一获得输入数据,逐步产生有序的输出序列.每步迭代中,算法从输入序列中取出一元素,将之插入有序序列中正确的位置.如此迭代直到全部元素有序. 归并排序进行如下迭 ...
- Xapian索引-文档检索过程分析
本文是Xapian检索过程的分析,本文内容中源码比较多.检索过程,总的来说就是拉取倒排链,取得合法doc,然后做打分排序的过程. 1 理论分析 1.1 检索语法 面对不同的检索业务,我们会有多种检索 ...
- Python内置函数(38)——list
英文文档: class list([iterable]) Rather than being a function, list is actually a mutable sequence type, ...
- 【转】Kali更新源
1.切换到root用户(如果已经是root用户就直接看第二步) dnt@Kali:~$ su 2.用vim打开sources.list,手动添加下面的更新源 root@Kali:~# vim /etc ...
- 使用ML.NET + Azure DevOps + Azure Container Instances打造机器学习生产化
介绍 Azure DevOps,以前称为Visual Studio Team Services(VSTS),可帮助个人和组织更快地规划,协作和发布产品.其中一项值得注意的服务是Azure Pipeli ...
- String str=null; 和String str=""的区别
1.最大的区别在于String str=null没有分配内存,String str=""分配了内存 2.String str=null 这个引用指向了一个null ,没有地址没 ...
- Chapter 5 Blood Type——3
Disappointment flooded through me as my eyes unerringly focused on his table. 当我的眼睛完全集中在他的桌上时,失望如洪水般 ...