C语言_文件操作相关练习题

描述

当前文章列出了4个文件编程相关的练习题。文件拷贝实现、 文件加密、学生管理系统链表模板(未添加文件操作)、学生管理系统模板(通过文件系统保存信息)、等4个例子。

1.1 文件拷贝实现

#include 
#include 
#include 
int main()
{
	/*1. 打开源文件*/
	FILE *file_src;
	FILE *file_new;
	unsigned char buff[1024];
	unsigned int cnt;

	file_src=fopen("D:/123.pdf","rb");
	if(file_src==NULL)
	{
		printf("源文件打开失败!\n");
		return -1;
	}
	/*2. 创建新文件*/
	file_new=fopen("D:/456.pdf","wb");
	if(file_new==NULL)
	{
		printf("新文件创建失败!\n");
		return -1;
	}
	/*3. 拷贝文件*/
	while(!feof(file_src))
	{
		cnt=fread(buff,1,1024,file_src);
		fwrite(buff,1,cnt,file_new);
	}
	/*4. 关闭文件*/
	fclose(file_new);
	fclose(file_src);
	printf("文件拷贝成功!\n");
	return 0;
}

1.2 文件加密

使用: 异或 ^
密码类型: (1) 整型密码 (2) 字符串密码
比如: 银行提款机的密码、QQ密码
加密代码:

#include 
#include 
#include 
int main()
{
	/*1. 打开源文件*/
	FILE *file_src;
	FILE *file_new;
	unsigned char buff[1024];
	unsigned int cnt;
	unsigned int password=123456; //密码数据
	unsigned int data;  //存放读取的数据

	file_src=fopen("D:/123.pdf","rb");
	if(file_src==NULL)
	{
		printf("源文件打开失败!\n");
		return -1;
	}
	/*2. 创建新文件*/
	file_new=fopen("D:/456.pdf","wb");
	if(file_new==NULL)
	{
		printf("新文件创建失败!\n");
		return -1;
	}
	/*3. 文件加密*/
	while(!feof(file_src))
	{
		cnt=fread(&data,1,4,file_src);
		data=data^password;//文件数据加密
		fwrite(&data,1,cnt,file_new);
	}
	/*4. 关闭文件*/
	fclose(file_new);
	fclose(file_src);
	printf("文件加密成功!\n");
	return 0;
}

解密代码:


#include 
#include 
#include 
int main()
{
	/*1. 打开源文件*/
	FILE *file_src;
	FILE *file_new;
	unsigned char buff[1024];
	unsigned int cnt;
	unsigned int password=123456; //密码数据
	unsigned int data;  //存放读取的数据

	file_src=fopen("D:/456.pdf","rb");
	if(file_src==NULL)
	{
		printf("源文件打开失败!\n");
		return -1;
	}
	/*2. 创建新文件*/
	file_new=fopen("D:/789.pdf","wb");
	if(file_new==NULL)
	{
		printf("新文件创建失败!\n");
		return -1;
	}
	/*3. 文件加密*/
	while(!feof(file_src))
	{
		cnt=fread(&data,1,4,file_src);
		data=data^password;//文件数据加密
		fwrite(&data,1,cnt,file_new);
	}
	/*4. 关闭文件*/
	fclose(file_new);
	fclose(file_src);
	printf("文件解密成功!\n");
	return 0;
}

1.3 学生管理系统链表模板(未添加文件操作)

#include 
#include 
#include 

//存放信息的结构体
struct MyStruct
{
	char Name[50]; //存放姓名
	int Number;    //存放编号
	struct MyStruct *next; //存放下一个节点的地址
};

//链表相关的函数接口
struct MyStruct *ListHead=NULL; //链表头
struct MyStruct *CreateListHead(struct MyStruct *head);
void AddrListInfo(struct MyStruct *head,struct MyStruct data);
void DeleteListInfo(struct MyStruct *head,int number);
void PrintListAllInfo(struct MyStruct *head);
int main()
{
	struct MyStruct data1={"张三",123};
	struct MyStruct data2={"李四",456};
	struct MyStruct data3={"小王",789};
	ListHead=CreateListHead(ListHead);
	
	//添加信息
	AddrListInfo(ListHead,data1);
	AddrListInfo(ListHead,data2);
	AddrListInfo(ListHead,data3);

	//删除节点
	DeleteListInfo(ListHead,123);
	DeleteListInfo(ListHead,789);

	//打印
	PrintListAllInfo(ListHead);
	return 0;
}
/*
函数功能: 创建链表头
*/
struct MyStruct *CreateListHead(struct MyStruct *head)
{
	if(head==NULL)
	{
		head=malloc(sizeof(struct MyStruct));
		head->next=NULL;
	}
	return head;
}

/*
函数功能: 在链表结尾添加节点
*/
void AddrListInfo(struct MyStruct *head,struct MyStruct data)
{
	struct MyStruct *p=head;
	struct MyStruct *tmp=NULL;

	while(p->next!=NULL)
	{
		p=p->next;
	}
	tmp=malloc(sizeof(struct MyStruct));
	memcpy(tmp,&data,sizeof(struct MyStruct));
	p->next=tmp;
	tmp->next=NULL;
}

/*
函数功能: 根据结构体里特有的成员区分进行删除链表节点信息
函数参数: int numbe  编号
*/
void DeleteListInfo(struct MyStruct *head,int number)
{
	struct MyStruct *p=head;
	struct MyStruct *tmp=NULL;

	while(p->next!=NULL)
	{
		tmp=p; //保存上一个节点的信息
		p=p->next;
		if(p->Number==number)
		{
			tmp->next=tmp->next->next;
			free(p);
			p=head; //链表头归位
		}
	}
}

/*
函数功能: 打印所有节点信息
函数参数: int numbe  编号
*/
void PrintListAllInfo(struct MyStruct *head)
{
	struct MyStruct *p=head;
	int cnt=0;
	printf("\n链表全部信息如下:\n");
	while(p->next!=NULL)
	{
		p=p->next;
		cnt++;
		printf("第%d个节点信息: %s,%d\n",cnt,p->Name,p->Number);
	}
}

1.4 学生管理系统模板(通过文件系统保存信息)

#include 
#include 
#include 

//存放信息的结构体
struct MyStruct
{
	char Name[50]; //存放姓名
	int Number;    //存放编号
	struct MyStruct *next; //存放下一个节点的地址
};

//链表相关的函数接口
struct MyStruct *ListHead=NULL; //链表头
struct MyStruct *CreateListHead(struct MyStruct *head);
void AddrListInfo(struct MyStruct *head,struct MyStruct data);
void DeleteListInfo(struct MyStruct *head,int number);
void PrintListAllInfo(struct MyStruct *head);

//文件操作相关函数
void SaveListAllInfo(struct MyStruct *head,char *path);
void GetListAllInfo(struct MyStruct *head,char *path);

#if 0
int main()
{
	struct MyStruct data1={"张三",123};
	struct MyStruct data2={"李四",456};
	struct MyStruct data3={"小王",789};
	ListHead=CreateListHead(ListHead);
	
	AddrListInfo(ListHead,data1);
	AddrListInfo(ListHead,data2);
	AddrListInfo(ListHead,data3);

	//保存节点信息
	SaveListAllInfo(ListHead,"D:/list.ini");

	//打印
	PrintListAllInfo(ListHead);
	return 0;
}
#endif

#if 1
int main()
{
	ListHead=CreateListHead(ListHead);

	//获取节点信息
	GetListAllInfo(ListHead,"D:/list.ini");

	//打印
	PrintListAllInfo(ListHead);
	return 0;
}
#endif

/*
函数功能: 创建链表头
*/
struct MyStruct *CreateListHead(struct MyStruct *head)
{
	if(head==NULL)
	{
		head=malloc(sizeof(struct MyStruct));
		head->next=NULL;
	}
	return head;
}

/*
函数功能: 在链表结尾添加节点
*/
void AddrListInfo(struct MyStruct *head,struct MyStruct data)
{
	struct MyStruct *p=head;
	struct MyStruct *tmp=NULL;

	while(p->next!=NULL)
	{
		p=p->next;
	}
	tmp=malloc(sizeof(struct MyStruct));
	memcpy(tmp,&data,sizeof(struct MyStruct));
	p->next=tmp;
	tmp->next=NULL;
}

/*
函数功能: 根据结构体里特有的成员区分进行删除链表节点信息
函数参数: int numbe  编号
*/
void DeleteListInfo(struct MyStruct *head,int number)
{
	struct MyStruct *p=head;
	struct MyStruct *tmp=NULL;

	while(p->next!=NULL)
	{
		tmp=p; //保存上一个节点的信息
		p=p->next;
		if(p->Number==number)
		{
			tmp->next=tmp->next->next;
			free(p);
			p=head; //链表头归位
		}
	}
}

/*
函数功能: 打印所有节点信息
函数参数: int numbe  编号
*/
void PrintListAllInfo(struct MyStruct *head)
{
	struct MyStruct *p=head;
	int cnt=0;
	printf("\n链表全部信息如下:\n");
	while(p->next!=NULL)
	{
		p=p->next;
		cnt++;
		printf("第%d个节点信息: %s,%d\n",cnt,p->Name,p->Number);
	}
}
/*
函数功能: 保存链表节点信息
*/
void SaveListAllInfo(struct MyStruct *head,char *path)
{
	struct MyStruct *p=head;
	FILE *file;
	file=fopen(path,"a+b");
	if(file==NULL)
	{
		printf("保存信息的文件打开失败!\n");
		return ;
	}
	while(p->next!=NULL)
	{
		p=p->next;
		fwrite(p,1,sizeof(struct MyStruct),file);
	}
	fclose(file);
}

/*
函数功能: 从文件里获取链表节点信息
*/
void GetListAllInfo(struct MyStruct *head,char *path)
{
	struct MyStruct *p=head;
	FILE *file;
	struct MyStruct data;

	file=fopen(path,"rb");
	if(file==NULL)
	{
		printf("保存信息的文件打开失败!\n");
		return;
	}
	//循环读取文件里的数据
	while(!feof(file))
	{
		fread(&data,1,sizeof(struct MyStruct),file); //读取链表节点数据
		AddrListInfo(head,data); //添加链表节点
	}
	fclose(file);
}
打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分