这是一个简易的学籍管理系统,大一时居然三个人写了一千多行......年少无知啊!欢迎摘果实!

  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++ 文件操作(简易的学籍管理系统)的更多相关文章

  1. 基于C语言文件操作的学生成绩管理系统

    原理 在一个班级学生成绩管理系统中,希望处理每个学生的学习情况信息,其中包括学生的学号.姓名.各科名称和成绩等并能使管理人员通过界面完成对学生信息的录入及对学生信息的录入及对数据的查找.浏览.插入.排 ...

  2. Qt实现学生学籍管理系统(文件存储)

    记录 19:53 2019-07-30 在小学期学c++做完课设后萌生了把写完的课设放在博客上的想法,于是,我第一篇博客诞生了. 22:32:19 2019-07-30 下棋 16:04:56 201 ...

  3. java课堂测试样卷-----简易学籍管理系统

    程序设计思路:分别建立两个类:ScoreInformation类(用来定义学生的基本信息以及设置set和get函数)ScoreManagement类(用来定义实现学生考试成绩录入,考试成绩修改,绩点计 ...

  4. 员工管理系统+字符编码+Python代码文件操作

    员工管理系统+字符编码+Python代码文件操作 1.员工管理系统 1.1  debug 代码调试 1.先使用鼠标左键在需要调试的代码左边点击一下(会出现一个红点)2.之后右键点击debug运行代码 ...

  5. android 文件操作类简易总结

    android 文件操作类(参考链接) http://www.cnblogs.com/menlsh/archive/2013/04/02/2997084.html package com.androi ...

  6. c++大作业--学籍管理系统--

    1.题目描写叙述 学籍管理系统: 依据信息管理系统的业务流程.要求以及所要实现的目标,完毕下面功能: (1)建立学生档案的管理和维护.实现计算机自己主动化管理体制. (2)建立学生成绩管理机制,在计算 ...

  7. 【C语言期末实训】学生学籍管理系统

    目录: 一,设计要求 ,总体要求: ,具体功能: 二,设计框架 三,程序代码 ,声明函数和头文件 ,声明结构体 ,声明全局变量 ,主体启动函数 ,主菜单函数 ,创建学生档案函数 ,编辑学生档案函数 , ...

  8. C++实现控制台学生学籍管理系统

    操作流程 创建文件 创建管理类 ​ 管理类负责的内容如下: 提供与用户的沟通菜单界面 实现对职工增删改查的操作 数组数据与文件的读写交互 菜单功能实现 在StudentManager.h中定义Show ...

  9. Django 小实例S1 简易学生选课管理系统 11 学生课程业务实现

    Django 小实例S1 简易学生选课管理系统 第11节--学生课程业务实现 点击查看教程总目录 作者自我介绍:b站小UP主,时常直播编程+红警三,python1对1辅导老师. 课程模块中,学生需要拥 ...

随机推荐

  1. [Swift]LeetCode646. 最长数对链 | Maximum Length of Pair Chain

    You are given n pairs of numbers. In every pair, the first number is always smaller than the second ...

  2. iReport 5.6.0 Error: net.sf.jasperreports.engine.JRException: Error executing SQL statement for : data 最优解决方案

    问题描述 近期学习iReport(个人使用的是最新版本的 iReport-5.6.0,MySQL是 5.5.56版本),遇到一些问题,在安装完成后,创建了数据库,配置了MySQL数据库连接信息,新建报 ...

  3. 2.Django路由规则

    路由规则 1.基于正则的url 在templates目录下创建index.html.detail.html文件 (1)index.html <!DOCTYPE html> <html ...

  4. 【转】关于 python ImportError: No module named 的问题

    今天在 centos 下安装 python setup.py install 时报错:ImportError: No module named sysconfig, 当时急着用,就顺手直接源码编译了一 ...

  5. 在.NET中使用Redis

    dll文件 namespace RedisDemo { public partial class RedisPage : System.Web.UI.Page { protected void Pag ...

  6. Ocelot + Consul实践

    关于Consul(https://www.consul.io)是一个分布式,高可用,支持多数据中心的服务发现和配置共享的服务软件,由 HashiCorp 公司用 Go 语言开发, 基于 Mozilla ...

  7. C++、Java语法差异对照表

    C++.Java语法差异对照表 C++ and Java Syntax Differences Cheat Sheet First, two big things--the main function ...

  8. MHA非root用户搭建测试

    最近一直在瞎搬砖,最大的感触是运维工作难做.不过废话不多说,最近被分配了一项比较有意思的task,尝试着非root用户搭建MHA并测试下能否成功漂移,以下是两天测试和文档编写的成果,分享给各位看客,欢 ...

  9. kubernetes的安装方法

    背景 自己学习k8s集群,无奈屌丝一枚,没钱配置vpn服务,安装k8s花费的时间太久了.为了小伙伴们可以快速安装k8s,我花了点时间整理了这篇博客,提供一个不用FQ就可以愉快安装k8s集群的方法. 主 ...

  10. 设计模式总结篇系列:适配器模式(Adapter)

    网上看到不少关于适配器模式的讲解,其中对于适配器模式解释的过于专业,一时不是特别理解适配器模式到底是用来干嘛的,具体的适用场景在哪,其最精髓的地方到底在哪. 本文结合自己的理解,阐述下对适配器模式的看 ...