本篇提供了关于C语言程序设计中文件部分的经典案例,可供C语言的初学者们进行一个学习和编译练习。
/*Case 1
保存学生信息
学生信息表中包含学号、姓名、年龄、性别四项信息,编程实现学生信息表的文本形式存储和二进制形式存储,并将生成的文件存储到D的Stu文件夹中*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct Student {
int sno;
char name[20];
int age;
char sex[5];
}Stu;
int main() {
FILE* fp1, * fp2;
Stu stu;
int no = 1001;
char name[20] = "张三";
int age = 18;
char sex[5] = "男";
//文本文件操作
fp1 = fopen("D:\\VS\\C语言经典案例\\ClassicCase\\TestFile\\file1.txt", "w");
if (fp1==NULL)
{
printf("无法打开文件file1.txt\n");
return -1;
}
fputs(name, fp1);
fputc('\n', fp1);
fprintf(fp1, "%d %s %d %s\n", no, name, age, sex);
fclose(fp1);
//二进制文件操作
fp2 = fopen("D:\\VS\\C语言经典案例\\ClassicCase\\TestFile\\file2.dat", "wb");
if (fp1 == NULL)
{
printf("无法打开文件file2.dat\n");
return -2;
}
stu.sno = 1001;
strcpy(stu.name, "张三");
stu.age = 20;
strcpy(stu.sex, "男");
fwrite(&stu, sizeof(Stu), 1, fp2);
fclose(fp2);
return 0;
}
/*Case 2
读取学生信息
从案例一生成的文件中,读取学生信息,输出到屏幕上*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct Student {
int sno;
char name[20];
int age;
char sex[5];
}Stu;
int main() {
FILE* fp1, * fp2;
Stu stu;
int no;
char name[20];
int age;
char sex[5];
char c;
Stu s;
fp1 = fopen("D:\\VS\\C语言经典案例\\ClassicCase\\TestFile\\file1.txt", "r");
if (fp1==NULL)
{
printf("无法打开文件file1.txt\n");
return -1;
}
printf("文本文件:\n");
fgets(name,10, fp1);
printf("第一行\n%s\n", name);
fscanf(fp1, "%d%s%d%s", &no, &name, &age, &sex);
printf("第二行:\n%-5d%-5s%-5d%-5s\n\n", no,name,age,sex);
fp2 = fopen("D:\\VS\\C语言经典案例\\ClassicCase\\TestFile\\file2.dat", "rb");
if (fp2 == NULL)
{
printf("无法打开文件file2.dat\n");
return -2;
}
fread(&s, sizeof(Stu), 1, fp2);
printf("二进制文件:\n");
printf("%d %s %d %s\n", s.sno, s.name, s.age, s.sex);
fclose(fp1);
fclose(fp2);
return 0;
}
/*Case 3
删除指定学生信息
(1)根据输入的路径和文件名创建或打开文件,通过输入设备输入多条学生信息,将输入的学生信息保存到磁盘文件中
(2)根据用户输入的学生姓名,删除成绩表中对应的记录*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Student {
char sname[20];
int sco;
}Stu,stu[20];
int main() {
FILE* fp1, * fp2;
int i, j, n, flag;
char name[10],filename[50];
printf("请输入文件名:\n");
scanf("%s", filename);
printf("请输入学生人数:\n");
scanf("%d",&n );
printf("请输入学生姓名和成绩:\n");
for (i = 0; i < n; i++)
{
printf("NO%d:\n", i + 1);
scanf("%s%d", stu[i].sname, &stu[i].sco);
}
if ((fp1 = fopen(filename, "ab")) == NULL)
{
printf("无法打开文件!");
return -1;
}
for (i = 0; i < n; i++)
{
if (fwrite(&stu[i],sizeof(Stu),1,fp1)!=1)
{
printf("error\n");
}
}
fclose(fp1);
if ((fp2 = fopen(filename, "rb")) == NULL)
{
printf("无法打开文件!");
return -1;
}
printf("\n初始数据表");
for ( i = 0; fread(&stu[i],sizeof(Stu),1,fp2) != 0; i++)
{
printf("\n%8s%7d", stu[i].sname, stu[i].sco);
}
n = i;
fclose(fp2);
start:
printf("\n请输入要删除的学生名:");
scanf("%s", name);
for ( flag = 1,i=0; flag&&i < n; i++)
{
if (strcmp(name,stu[i].sname)==0)
{
for ( j = i; j < n-i; j++)
{
strcpy(stu[j].sname, stu[j + 1].sname);
stu[j].sco = stu[j + 1].sco;
}
flag = 0;
}
}
if (!flag)
{
n = n - 1;
}
else
{
printf("\n学生信息未找到!");
goto start;
}
printf("\n当前数据表");
fp2 = fopen(filename, "wb");
for ( i = 0; i < n; i++)
{
fwrite(&stu[i], sizeof(Stu), 1, fp2);
}
fclose(fp2);
fp2 = fopen(filename, "rb");
for (i = 0; fread(&stu[i],sizeof(Stu),1,fp2) != 0; i++)
{
printf("\n%8s%7d",stu[i].sname, stu[i].sco);
}
fclose(fp2);
printf("\n");
return 0;
}
/*Case 4
综合案例——文件加密
对已存在的文件进行加密和解密*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//加密函数
void encrypt(char* sfile, char* cfile)
{
printf("文件加密中……\n");
int i = 0;
//定义两个文件指针,分别指向源文件和加密后的文件
FILE* fp1, * fp2;
int ch; //记录从源文件中获取的字符
//以只读方式打开源文件
fp1 = fopen(sfile, "rb");
if (fp1==NULL)
{
printf("无法打开源文件%s\n", sfile);
return;
}
//以只写的方式打开加密文件
fp2 = fopen(cfile, "wb");
if (fp2 == NULL)
{
printf("无法打开源文件%s\n", cfile);
return;
}
ch = fgetc(fp1); //从源文件中读取一个字符
while (!feof(fp1))
{
//采用异或方式,使用密码“123+i”对字符逐个加密
ch = (123 + i) ^ ch;
i++;
fputc(ch, fp2); //将加密后的字符写入加密文件fp2
ch = fgetc(fp1); //继续从源文件中获取字符
if (i>5)
{
i = 0;
}
}
printf("加密完成!\n");
//关闭文件
fclose(fp1);
fclose(fp2);
}
//解密函数
void decrypt(char* cfile, char* dfile)
{
int i = 0;
//定义两个文件指针,分别指向加密文件和解密后的文件
FILE* fp1, * fp2;
int ch;
//以只读方式打开加密文件
fp1 = fopen(cfile, "rb");
if (fp1 == NULL)
{
printf("无法打开加密文件%s\n", cfile);
return;
}
//以只写的方式打开解密后的文件
fp2 = fopen(dfile, "wb");
if (fp2 == NULL)
{
printf("无法打开解密后的文件%s\n", cfile);
return;
}
ch = fgetc(fp1); //获取文件fp1的字符
while (!feof(fp1))
{
//对已加密的字符逐个解密
ch = (123 + i) ^ ch;
i++;
fputc(ch, fp2); //将解密后的字符写入文件fp2
ch = fgetc(fp1);
if (i > 5)
{
i = 0;
}
}
//关闭文件
fclose(fp1);
fclose(fp2);
}
int main() {
//定义源文件、加密文件、解密后的文件
char sourcefile[50];
char codefile[50];
char decodefile[50];
printf("请输入源文件的文件名:\n");
gets(sourcefile);
printf("请输入加密文件的文件名:\n");
gets(codefile);
//调用加密函数对文件加密
encrypt(sourcefile, codefile);
//文件解密
printf("请输入解密后的文件名“\n");
gets(decodefile);
//调用解密函数对加密文件进行加密
decrypt(codefile, decodefile);
return 0;
}
/*Case 5
综合案例——图书管理系统
实现一个基于单链表的图书管理系统,该系统可以实现图书信息的增加、浏览、查询、更新、删除这五项功能,并能将链表中存储的数据保存到文件中*/
StuManage.h文件
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
typedef struct book
{
char bnum[10]; //书籍编号
char bname[30]; //书名
char bauthor[20]; //作者
char bclassfy[10]; //类别编号
float bprice; //价格
struct book* next; //链表指针
}BookInfo;
BookInfo* CreateBooksList(); //创建链表
void Insert(BookInfo* head); //插入
void Delete(BookInfo* head); //删除
void Print(BookInfo* head); //浏览
void Search(BookInfo* head); //查询
void Update(BookInfo* head); //修改
void Save(BookInfo* head); //保存
int menu(); //菜单
StuManage.c文件
#define _CRT_SECURE_NO_WARNINGS 1
#include "StuManage.h"
#include <stdlib.h>
#include <string.h>
//创建书单
BookInfo* CreateBooksList()
{
BookInfo* head;
head = (BookInfo*)malloc(sizeof(BookInfo)); //为头节点分配空间
head->next = NULL; //初始化头指针
return head;
}
//插入记录
void Insert(BookInfo* head)
{
BookInfo* b, * p;
char flag = 'Y';
p = head;
while (p->next!=NULL)
{
p = p->next;
}
while (flag=='Y'||flag=='y')
{
b = (BookInfo*)malloc(sizeof(BookInfo));
printf("请输入图书编号:");
fflush(stdin);
scanf("%s", b->bnum);
printf("请输入书名:");
fflush(stdin);
scanf("%s", b->bname);
printf("请输入作者:");
fflush(stdin);
scanf("%s", b->bauthor);
printf("请输入类别编号:");
fflush(stdin);
scanf("%s", b->bclassfy);
printf("请输入图书价格:");
fflush(stdin);
scanf("%f", &b->bprice);
p->next = b;
p = b;
b->next = NULL;
printf("添加成功!\n继续添加?(Y/N)");
fflush(stdin);
scanf("%c", &flag);
if (flag=='N'||flag=='n')
{
break;
}
else if(flag=='Y'||flag=='y')
{
continue;
}
}
return;
}
//删除记录
void Delete(BookInfo* head)
{
BookInfo* b, * p;
char tmp[30];
int flag;
flag = 0;
b = head;
p = head;
printf("请输入要删除的书名:");
fflush(stdin);
scanf("%s", tmp);
//遍历链表
while (p != NULL)
{
if (strcmp(p->bname,tmp)==0)
{
flag = 1;
break;
}
p = p->next;
}
if (flag==1)
{
for ( ; b->next!=p; )
{
b = b->next;
}
b->next = p->next;
free(p);
printf("删除成功!\n");
}
else
{
printf("该书不存在!");
}
return;
}
//浏览书单
void Print(BookInfo* head)
{
BookInfo* p;
if (head == NULL || head->next == NULL)
{
printf("无记录!\n");
return;
}
p = head;
printf("************************************\n");
printf("**编号**书名**作者**类别编号**价格**\n");
printf("************************************\n");
//遍历链表,输出书籍信息
while (p->next != NULL)
{
p = p->next;
printf("**%-6s**%-10s**%-10s**%-10s**%.21f**\n",
p->bnum,p->bname,p->bauthor,p->bclassfy,p->bprice);
printf("************************************\n");
}
}
//查找书籍
void Search(BookInfo*head)
{
BookInfo* p;
char tmp[30];
int flag=0;
p = head;
if (head == NULL || head->next == NULL)
{
printf("清单为空!\n");
}
else
{
printf("请输入书名:");
fflush(stdin);
scanf("%s", tmp);
while (p->next!=NULL)
{
p = p->next;
if (strcmp(p->bname,tmp)==0)
{
flag = 1;
printf("编号:%s\n书名:《%s》\n作者:%s\n分类:%s\n价格:%.2f\n",
p->bnum, p->bname, p->bauthor, p->bclassfy, p->bprice);
return;
}
if (p->next==NULL)
{
printf("\n查询完成!");
}
}
if (flag == 0)
{
printf("没有找到《%s》!\n", tmp);
}
}
return;
}
//修改信息
void Update(BookInfo* head)
{
BookInfo* p;
char tmp[30];
int flag = 0;
p = head;
printf("请输入书名:");
fflush(stdin);
scanf("%s", tmp);
while (p->next != NULL)
{
p = p->next;
if (strcmp(p->bname, tmp) == 0)
{
flag = 1;
printf("请输入图书编号:");
fflush(stdin);
scanf("%s", p->bnum);
printf("请输入书名:");
fflush(stdin);
scanf("%s", p->bname);
printf("请输入作者:");
fflush(stdin);
scanf("%s", p->bauthor);
printf("请输入类别编号:");
fflush(stdin);
scanf("%s", p->bclassfy);
printf("请输入图书价格:");
fflush(stdin);
scanf("%f", &p->bprice);
}
}
if (flag == 0)
{
printf("没有找到《%s》!\n", tmp);
}
}
//保存书单到文件
void Save(BookInfo* head)
{
BookInfo* p;
FILE* fp;
p = head;
//以只写的方式打开文件
fp = fopen("D:\\VS\\C语言经典案例\\ClassicCase\\ClassicCase\\bookslist.txt","w");
fprintf(fp,"************************************\n");
fprintf(fp,"**编号**书名**作者**类别编号**价格**\n");
fprintf(fp,"************************************\n");
//遍历链表,输出书籍信息
while (p->next != NULL)
{
p = p->next;
fprintf(fp,"**%-6s**%-10s**%-10s**%-10s**%.21f**\n",
p->bnum, p->bname, p->bauthor, p->bclassfy, p->bprice);
fprintf(fp,"************************************\n");
}
fclose(fp);
printf("保存成功!\n");
printf("数据已成功保存到D:\\VS\\C语言经典案例\\ClassicCase\\ClassicCase\\bookslist.txt","w");
}
//菜单
int menu()
{
int sec;
printf(" 图书管理系统 \n");
printf("————————————————————\n");
printf(" 1-图书信息录入 \n");
printf(" 2-图书信息浏览 \n");
printf(" 3-图书信息查询 \n");
printf(" 4-图书信息修改 \n");
printf(" 5-图书信息删除 \n");
printf(" 6-图书信息保存 \n");
printf(" 7-退出 \n");
printf("————————————————————\n");
printf("请选择:");
fflush(stdin);
scanf("%d", &sec);
while (sec>7||sec<0)
{
printf("选择有误!\n请重新输入:");
scanf("%d", &sec);
}
return sec;
}
main02.c文件
#define _CRT_SECURE_NO_WARNINGS 1
#include"StuManage.h"
#include<Windows.h>
int main() {
BookInfo* head;
int sel;
head = NULL;
for( ; ; )
{
sel = menu();
switch (sel)
{
case 1:
if (head==NULL)
{
head = CreateBooksList();
}
Insert(head);
break;
case 2:
Print(head); break;
case 3:
Search(head); break;
case 4:
Update(head); break;
case 5:
Delete(head); break;
case 6:
Save(head); break;
case 7:
exit(0); break;
default:
break;
}
}
return 0;
}
本篇的所有代码是在VS编译环境下操作的,如有错误,望大家指正!!!