标准IO基本函数的使用

思维导图:

1:使用 fputc 和 fgetc 实现文件的拷贝功能

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>

typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;

int main(int argc, const char *argv[])
{
	FILE* rfp=fopen(argv[1],"r");
	FILE* wfp=fopen(argv[2],"w");
	if(rfp == NULL)
	{
		perror("fopen");
		return 1;
	}
	if(wfp == NULL)
	{
		perror("fopen");
		return 1;
	}
	while(1)
	{
		int res=fgetc(rfp);
		if(res == EOF)
		{
			break;
		}
		fputc(res,wfp);
	}
	fclose(rfp);
	fclose(wfp);
	return 0;
}

2:将结构体数组的加载保存的代码,把结构体数组改成链表再来一次

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>

typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;

typedef struct student
{
	char name[20];
	int chinese;
	int math;
	int english;
	int physics;
	int chemistry;
	int biology;
}stu;
typedef struct node
{
	union
	{
		int length;
		stu s;
	};
	struct node* next;
}node,*node_p;
//创建头节点
node_p create()
{
	node_p H=(node_p)malloc(sizeof(node));
	if(NULL == H)
	{
		printf("链表创建失败!\n");
		return NULL;
	}
	H->length=0;
	H->next=NULL;
	return H;
}
//创建普通节点
node_p create_node(char name[],int chinese,int math,int english,int physics,int chemistry,int biology)
{
	node_p p=(node_p)malloc(sizeof(node));
	if(NULL == p)
	{
		printf("节点创建失败!\n");
		return NULL;
	}
	strcpy(p->s.name,name);
	p->s.chinese=chinese;
	p->s.math=math;
	p->s.english=english;
	p->s.physics=physics;
	p->s.chemistry=chemistry;
	p->s.biology=biology;
	p->next=NULL;
	return p;
}
//尾插
void insert_tail(node_p H,char name[],int chinese,int math,int english,int physics,int chemistry,int biology)
{
	if(NULL == H)
	{
		printf("插入数据失败!\n");
		return;
	}
	node_p p=create_node(name,chinese,math,english,physics,chemistry,biology);
	node_p q=H;
	while(q->next!=NULL)
	{
		q=q->next;
	}
	q->next=p;
	H->length++;
}
//遍历显示
void show(node_p H)
{
	if(NULL == H)
	{
		printf("遍历失败!\n");
		return;
	}
	node_p q=H;
	while(q->next!=NULL)
	{
		q=q->next;
		printf("%s %d %d %d %d %d %d\n",q->s.name,q->s.chinese,q->s.math,q->s.english,q->s.physics,q->s.chemistry,q->s.biology);
	}
}
//尾删
void delete_tail(node_p H)
{
	if(NULL == H)
	{
		printf("删除失败!\n");
		return;
	}
	node_p q=H;
	for(int i=0;i<H->length-1;i++)
	{
		q=q->next;//要删除尾节点的前一个节点
	}
	node_p p=q->next;//要删除的尾节点
	q->next=p->next;
	free(p);
	p=NULL;
	H->length--;
}
int main(int argc, const char *argv[])
{
	node_p H=create();;
	insert_tail(H,"小明",10,10,10,10,10,10);
	insert_tail(H,"小华",20,20,20,20,20,20);
	insert_tail(H,"小兰",30,30,30,30,30,30);
	insert_tail(H,"小刚",40,40,40,40,40,40);
	insert_tail(H,"小红",50,50,50,50,50,50);
	show(H);
	putchar(10);
	FILE* wfp=fopen(argv[1],"a");
	node_p q=H;
	while(q->next!=NULL)
	{
		q=q->next;
		//把链表中的学生信息存到文件中
		fprintf(wfp,"%s %d %d %d %d %d %d\n",q->s.name,q->s.chinese,q->s.math,q->s.english,q->s.physics,q->s.chemistry,q->s.biology);
	}
	fclose(wfp);
	delete_tail(H);//删除链表中的所有学生信息
	delete_tail(H);
	delete_tail(H);
	delete_tail(H);
	delete_tail(H);
	show(H);
	putchar(10);
	//printf("链表已为空!\n");
	FILE* rfp=fopen(argv[1],"r");
	int i=0;
	while(1)
	{
		stu data[5]={0};
		//把文件中存储的学生信息再放回到链表当中
		fscanf(rfp,"%s %d %d %d %d %d %d\n",data[i].name,&data[i].chinese,&data[i].math,&data[i].english,&data[i].physics,&data[i].chemistry,&data[i].biology);
		insert_tail(H,data[i].name,data[i].chinese,data[i].math,data[i].english,data[i].physics,data[i].chemistry,data[i].biology);
		if(feof(rfp) == 1)
		{
			break;
		}
		i++;
	}
	fclose(rfp);
	show(H);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值