信息管理系统 Day02

那就写个信息管理系统

前情提要
昨天(2020.0512)从一个简单的乘法口诀表的生成,一直扩展到了界面的转换,用switch case 代替 if else

然后,定睛一看,这不就奔着一个信息管理系统在写么。

所以。。。那就写个信息管理系统吧。
先贴上昨天的代码信息系统的萌芽哈哈哈(见链接末尾)

分析
信息管理系统,需要对信息进行存储、查找、插入、删除这几个基本功能。
首先需要的就是c语言的文件功能。

下面学习一些C语言文件的基本操作。
一、文件写入
首先函数原型为_CRTIMP FILE * __cdecl fopen(const char *, const char *);
先调用FILE 定义一个指针fp
然后再对fp进行操作
fopen(“文件地址及文件名”,“操作名”)
文件地址及文件名: 如没有文件,则只填写地址,如下
操作名:
“r”:只能从文件中读数据,该文件必须先存在,否则打开失败
“w”:只能向文件写数据,若指定的文件不存在则创建它,如果存在则先删除它再重建一个新文件
“a”:向文件增加新数据(不删除原有数据),若文件不存在则打开失败,打开时位置指针移到文件末尾
“r+”:可读/写数据,该文件必须先存在,否则打开失败
“w+”:可读/写数据,用该模式打开新建一个文件,先向该文件写数据,然后可读取该文件中的数据
“a+”:可读/写数据,原来的文件不被删去,位置指针移到文件末尾

现在进行一个文件的创建

FILE* fp;
	fp = fopen("C:\\Users\\杨文峰\\Desktop\\Text\\text.txt", "w");

建议大家在写代码的时候,如果不知道某条语句是否成功执行,就在后面插个眼printf("运行成功");这样很容易就知道了。

文件创建好了,写入些东西吧

while ((ch = getchar()) != '0') {
		fputc(ch, fp);
	}
	printf("文件写入成功");

通过fputc()语句可以将字符写入指针fp所指向的文件中
效果就不贴图了,来回截图好麻烦,这一步骤很简单的。自己试试。
(这种类似介绍性话语就是防止我哪天突然完全忘记咋编代码而准备的)

文件写入了,接下来读出试试

字符读取函数fgetc()可从文件数据流中一次读取一个字符,然后读取光标移动到下一个字符,并逐步将文件的内容读出。

如果字符读取成功,则返回所读取的字符,否则返回EOF(end of file)。EOF是表示数据结尾的常量,真值为-1。另外,要判断文件是否读取完毕,可利用feof()进行检查。未完返回0,已完返回非零值。
feof()函数原型为:_CRTIMP int __cdecl feof(FILE *);
————————————————————
内容摘自https://blog.csdn.net/tjcwt2011/article/details/81125912
原作者致守

fp = fopen("C:\\Users\\杨文峰\\Desktop\\Text\\text.txt", "r");
	if (fp != NULL)
	{
		while (!feof(fp))
			printf("%c", fgetc(fp));
	}
	else
		printf("文件不存在!!!");

为了使代码结构清晰明了
分别建立fileput()和fileget()

void fileput(FILE *fp)//字符写入
{
	char ch;
	while ((ch = getchar()) != '0') {
		fputc(ch, fp);
	}
	printf("文件写入成功");

}
void fileget(FILE *fp)//字符读取
{
	fp = fopen("C:\\Users\\杨文峰\\Desktop\\Text\\text.txt", "r");
	if (fp != NULL)
	{
		while (!feof(fp))
			printf("%c", fgetc(fp));
	}
	else
		printf("文件不存在!!!");
}

运行后发现无法读取文件
怀疑是写入的时候没有关闭所以没有保存
插入关闭函数fclose(fp);
读取成功

跳过scanf解决办法:在scanf前加getchar();

#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS;
//文件相关练习
#include"stdio.h"
#include "stdlib.h"
#include "conio.h"
void usercontral();
void filecreate();
void fileput();
void fileget();
void filecreate()//文件创建
{
	FILE* fp;
	fp = fopen("C:\\Users\\杨文峰\\Desktop\\Text\\text.txt", "w");
	printf("文件创建成功\n");
	printf("是否返回菜单?是1");
	int j;
	scanf_s("%d", &j);
	
	if (j == 1) {
		system("cls");
		usercontral();
	}
	else exit(0);
	
}
void fileput(FILE *fp)//字符写入
{
	fp = fopen("C:\\Users\\杨文峰\\Desktop\\Text\\text.txt", "a");
	printf("请写入\n");
	char ch;
	while ((ch = getchar()) != '0') {
		fputc(ch, fp);
	}
	
	printf("文件写入成功\n");
	fclose(fp);
	printf("是否返回菜单?是1");
	int j;
	scanf_s("%d", &j);

	if (j == 1) {
		system("cls");
		usercontral();
	}
	else exit(0);
}
void fileget(FILE *fp)//字符读取
{
	fp = fopen("C:\\Users\\杨文峰\\Desktop\\Text\\text.txt", "r");
	if (fp != NULL)
	{
		while (!feof(fp))
			printf("%c", fgetc(fp));
		printf("文件读取成功\n"); 

	}
	else
		printf("文件不存在!!!\n");
	fclose(fp);
	printf("是否返回菜单?是1");
	int j;
	scanf_s("%d", &j);

	if (j == 1) {
		system("cls");
		usercontral();
	}
	else exit(0);
}
void usercontral()
{
	FILE* fp;
	fp = fopen("C:\\Users\\杨文峰\\Desktop\\Text\\text.txt", "r+");//设置可读写模式
	
	int ch;
	printf("0创建文件\n");
	printf("1写入文件\n");
	printf("2读取文件\n");
	printf("请选择输入:");
	/*scanf_s("%c", &ch,sizeof(ch));*/
	printf("检查\n");
	getchar();
	scanf_s("%d", &ch);

	switch (ch) {
	case 0:system("cls"); filecreate(); break;
	case 1:system("cls"); fileput(fp);break;
	case 2:system("cls"); fileget(fp); break;
	default:printf("请输入0-2");
	}
	
}
int main()
{
	//FILE* fp;
	///*filecreate();*/
	//fp = fopen("C:\\Users\\杨文峰\\Desktop\\Text\\text.txt", "w");
	//printf("文件创建成功");
	//fileput(fp);
	//fileget(fp);
	usercontral();
	
	

}

以上就实现了基本的文件读写操作

下面来建立信息库

待续。。。。。

学习参考https://blog.csdn.net/tjcwt2011/article/details/81125912
作者致守,多谢大佬分享。小白感激不尽!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的职工考勤管理系统的C++代码示例,其中包括员工信息管理、考勤管理、统计查询和用户管理等模块: ```c++ #include <iostream> #include <fstream> #include <string> #include <vector> #include <ctime> using namespace std; // 定义员工类 class Employee { public: Employee(int id, string name, string gender, string department, string level) { this->id = id; this->name = name; this->gender = gender; this->department = department; this->level = level; } int getId() { return id; } string getName() { return name; } string getGender() { return gender; } string getDepartment() { return department; } string getLevel() { return level; } private: int id; string name; string gender; string department; string level; }; // 定义考勤类 class Attendance { public: Attendance(Employee* employee, int year, int month, int day, int hour, int minute, string type) { this->employee = employee; this->year = year; this->month = month; this->day = day; this->hour = hour; this->minute = minute; this->type = type; } Employee* getEmployee() { return employee; } int getYear() { return year; } int getMonth() { return month; } int getDay() { return day; } int getHour() { return hour; } int getMinute() { return minute; } string getType() { return type; } private: Employee* employee; int year; int month; int day; int hour; int minute; string type; }; // 定义节假日类 class Holiday { public: Holiday(int year, int month, int day, string name) { this->year = year; this->month = month; this->day = day; this->name = name; } int getYear() { return year; } int getMonth() { return month; } int getDay() { return day; } string getName() { return name; } private: int year; int month; int day; string name; }; // 定义部门类 class Department { public: Department(int id, string name) { this->id = id; this->name = name; } int getId() { return id; } string getName() { return name; } private: int id; string name; }; // 定义用户类 class User { public: User(string username, string password, string role) { this->username = username; this->password = password; this->role = role; } string getUsername() { return username; } string getPassword() { return password; } string getRole() { return role; } private: string username; string password; string role; }; // 定义抽象类 class AbstractClass { public: virtual void add() = 0; virtual void remove() = 0; virtual void update() = 0; virtual void query() = 0; }; // 定义员工信息管理类 class EmployeeManager : public AbstractClass { public: void add() { // 添加员工信息 } void remove() { // 删除员工信息 } void update() { // 更新员工信息 } void query() { // 查询员工信息 } }; // 定义考勤管理类 class AttendanceManager : public AbstractClass { public: void add() { // 添加考勤信息 } void remove() { // 删除考勤信息 } void update() { // 更新考勤信息 } void query() { // 查询考勤信息 } }; // 定义统计查询类 class StatisticManager : public AbstractClass { public: void add() { // 添加统计信息 } void remove() { // 删除统计信息 } void update() { // 更新统计信息 } void query() { // 查询统计信息 } }; // 定义用户管理类 class UserManager : public AbstractClass { public: void add() { // 添加用户信息 } void remove() { // 删除用户信息 } void update() { // 更新用户信息 } void query() { // 查询用户信息 } }; // 定义文件读写类 class FileUtil { public: static vector<string> read(string filename) { vector<string> lines; ifstream fin(filename); if (fin) { string line; while (getline(fin, line)) { lines.push_back(line); } fin.close(); } return lines; } static void write(string filename, vector<string> lines) { ofstream fout(filename); if (fout) { for (string line : lines) { fout << line << endl; } fout.close(); } } }; // 定义系统类 class System { public: System() { // 初始化系统 } void login(string username, string password) { // 用户登录 } void logout() { // 用户退出 } void run() { // 运行系统 } private: vector<Employee*> employees; vector<Attendance*> attendances; vector<Holiday*> holidays; vector<Department*> departments; vector<User*> users; }; int main() { System system; system.run(); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值