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]LeetCode206. 反转链表 | Reverse Linked List
Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4-> ...
- [Swift]LeetCode703. 数据流中的第K大元素 | Kth Largest Element in a Stream
Design a class to find the kth largest element in a stream. Note that it is the kth largest element ...
- [Swift]LeetCode878. 第 N 个神奇数字 | Nth Magical Number
A positive integer is magical if it is divisible by either A or B. Return the N-th magical number. ...
- iOS模拟器使用
在iOS开发过程中一直都是使用模拟器进行调试,在模拟器上有很多不适应的地方,但是其实在模拟器上也有很多其他的功能,在本文中主要对模拟器的一些基本功能进行总结一下. 1 首先,我们了解一下模拟器中常用的 ...
- Lunix服务器上项目迁移命令
scp就是security copy,用于将文件或者目录从一个Linux系统拷贝到另一个Linux系统下. scp传输数据用的是SSH协议,保证了数据传输的安全,其格式如下: scp 远程用户名@IP ...
- Python内置函数(51)——property
英文文档: class property(fget=None, fset=None, fdel=None, doc=None) Return a property attribute. fget is ...
- BBS论坛(四)
4.1.cms登录页面csrf保护 (1)Perfect_bbs.py from flask_wtf import CSRFProtect CSRFProtect(app) 添加csrf保护后,现在再 ...
- Git的使用--如何将本地项目上传到Github(三种简单、方便的方法)
一.第一种方法: 1.首先你需要一个github账号,所以还没有的话先去注册吧! https://github.com/ 我们使用git需要先安装git工具,这里给出下载地址,下载后一路(傻瓜式安装) ...
- 死磕 java集合之CopyOnWriteArrayList源码分析
欢迎关注我的公众号"彤哥读源码",查看更多源码系列文章, 与彤哥一起畅游源码的海洋. 简介 CopyOnWriteArrayList是ArrayList的线程安全版本,内部也是通过 ...
- 转:OAuth 2.0 介绍
OAuth是一个关于授权(authorization)的开放网络标准,在全世界得到广泛应用,目前的版本是2.0版. 本文对OAuth 2.0的设计思路和运行流程,做一个简明通俗的解释,主要参考材料为R ...