用C语言怎么简单搞定数据库的增删改查操作,手把手教你一步步实现
- 问答
- 2026-01-01 05:43:29
- 3
我们要明确一点,用C语言“简单搞定”数据库,通常不是指去连接像MySQL、PostgreSQL那样的大型专业数据库,而是指我们自己动手实现一个非常非常简单的、基于文件的微型“数据库”,说白了,就是学习如何用C语言把结构化的数据保存到文件里,然后再能读出来、改一改、删掉,理解了这套逻辑,以后再去学专业的数据库库函数(比如MySQL C API)就会轻松很多。
我们的目标是:管理一个“学生信息表”,每个学生有学号(ID)、姓名(Name)和年龄(Age)。
第一步:定义数据结构(来源:C语言结构体基础)
任何数据操作都得先有结构,在C语言里,我们用结构体(struct)来定义一条“记录”。
#include <stdio.h>
#include <string.h>
// 定义一个学生结构体,这就是我们的一条记录
struct Student {
int id; // 学号,作为唯一标识(主键)
char name[50]; // 姓名
int age; // 年龄
};
第二步:增(Create) - 添加新记录(来源:文件操作fwrite函数)

“增”就是向数据库里添加新数据,就是把一个结构体数据追加(Append)到文件末尾。
void addStudent() {
struct Student stu;
FILE *file = fopen("database.dat", "ab"); // 以追加二进制模式打开文件
if (file == NULL) {
printf("文件打开失败!\n");
return;
}
printf("请输入学号: ");
scanf("%d", &stu.id);
printf("请输入姓名: ");
scanf("%s", stu.name); // 简单起见,假设姓名无空格
printf("请输入年龄: ");
scanf("%d", &stu.age);
// 将整个结构体写入文件
fwrite(&stu, sizeof(struct Student), 1, file);
fclose(file);
printf("学生信息添加成功!\n");
}
关键点:用 "ab" 模式打开文件,a 表示追加,不会覆盖旧数据;b 表示二进制模式,这样写进去的数据最原始,不会因为遇到换行符等出问题。fwrite 函数一次性把整个结构体写入文件,非常方便。
第三步:查(Read) - 显示所有记录(来源:文件操作fread函数)
“查”就是把文件里的所有数据都读出来并显示在屏幕上。

void listAllStudents() {
struct Student stu;
FILE *file = fopen("database.dat", "rb"); // 以只读二进制模式打开
if (file == NULL) {
printf("文件不存在或打开失败!\n");
return;
}
printf("所有学生信息:\n");
printf("学号\t姓名\t年龄\n");
printf("-------------------\n");
// 循环读取文件,直到文件末尾
while (fread(&stu, sizeof(struct Student), 1, file) == 1) {
printf("%d\t%s\t%d\n", stu.id, stu.name, stu.age);
}
fclose(file);
}
关键点:用 "rb" 模式只读打开。fread 函数会尝试从文件里读取一个结构体大小的数据,如果读成功了,返回值是1(因为我们要求读1个元素);如果读到文件末尾,读不到东西了,返回值就是0,循环就结束了。
第四步:改(Update) - 修改已有记录(来源:文件定位fseek和ftell函数)
“改”是稍微复杂一点的操作,因为文件是顺序的,我们不能直接跳到某条记录上修改,通常的做法是:
- 打开原文件和一个临时文件。
- 逐条读取原文件的记录。
- 如果是要修改的记录,就向临时文件写入新数据;如果不是,就向临时文件写入原数据。
- 最后删除原文件,把临时文件重命名为原文件的名字。
void updateStudent() {
int targetId;
struct Student stu;
int found = 0;
printf("请输入要修改学生的学号: ");
scanf("%d", &targetId);
FILE *file = fopen("database.dat", "rb");
FILE *tempFile = fopen("temp.dat", "wb"); // 创建临时文件
if (file == NULL || tempFile == NULL) {
printf("文件操作失败!\n");
return;
}
while (fread(&stu, sizeof(struct Student), 1, file)) {
if (stu.id == targetId) {
found = 1;
printf("找到学生:%s, 年龄:%d\n", stu.name, stu.age);
printf("请输入新的姓名: ");
scanf("%s", stu.name);
printf("请输入新的年龄: ");
scanf("%d", &stu.age);
printf("信息已更新!\n");
}
// 无论是原数据还是新数据,都写入临时文件
fwrite(&stu, sizeof(struct Student), 1, tempFile);
}
fclose(file);
fclose(tempFile);
// 文件替换操作
remove("database.dat"); // 删除原文件
rename("temp.dat", "database.dat"); // 将临时文件重命名为原文件
if (!found) {
printf("未找到学号为 %d 的学生,\n", targetId);
}
}
第五步:删(Delete) - 删除记录(来源:同上)

“删”的操作和“改”非常像,甚至更简单,思路一模一样,只是遇到要删除的记录时,我们不把它写入临时文件就行了。
void deleteStudent() {
int targetId;
struct Student stu;
int found = 0;
printf("请输入要删除学生的学号: ");
scanf("%d", &targetId);
FILE *file = fopen("database.dat", "rb");
FILE *tempFile = fopen("temp.dat", "wb");
if (file == NULL || tempFile == NULL) {
printf("文件操作失败!\n");
return;
}
while (fread(&stu, sizeof(struct Student), 1, file)) {
if (stu.id == targetId) {
found = 1;
printf("已删除学生:%s\n", stu.name);
} else {
// 只把不需要删除的记录写入临时文件
fwrite(&stu, sizeof(struct Student), 1, tempFile);
}
}
fclose(file);
fclose(tempFile);
remove("database.dat");
rename("temp.dat", "database.dat");
if (!found) {
printf("未找到学号为 %d 的学生,\n", targetId);
}
}
做一个简单的菜单(来源:C语言switch-case和循环)
把上面这些函数组合起来,形成一个可以交互的小程序。
int main() {
int choice;
while (1) {
printf("\n--- 简单学生信息管理系统 ---\n");
printf("1. 添加学生\n");
printf("2. 显示所有学生\n");
printf("3. 修改学生信息\n");
printf("4. 删除学生\n");
printf("5. 退出\n");
printf("请选择操作: ");
scanf("%d", &choice);
switch (choice) {
case 1: addStudent(); break;
case 2: listAllStudents(); break;
case 3: updateStudent(); break;
case 4: deleteStudent(); break;
case 5: printf("再见!\n"); return 0;
default: printf("无效选择!\n");
}
}
return 0;
}
这就是用C语言搞定最基本增删改查的全过程,它的核心就是把内存中的结构体通过 fwrite 和 fread 映射到磁盘文件上,这个方法非常原始,有很多缺点(比如效率低、无法复杂查询、数据冗余大),但它清晰地揭示了数据库最底层的工作原理,当你明白了这个“玩具数据库”是怎么运行的,再去学习使用SQLite(一个用C写的轻量级数据库)或者MySQL的C语言接口时,你就会发现,那些接口函数本质上就是在帮你更高效、更安全地完成我们上面手写的这些步骤。
本文由颜泰平于2026-01-01发表在笙亿网络策划,如有疑问,请联系我们。
本文链接:https://haoid.cn/wenda/72276.html
