IO 0715

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

#include <stdio.h>	
#include <string.h>
#include <unistd.h>
#include <stdlib.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* fp=fopen(argv[1],"r");
	FILE* fs=fopen(argv[2],"w");
	if(fp==NULL||fs==NULL)
	{
		perror("fopen");
		return 1;
	}
	while(1)
	{
		unsigned char ch=fgetc(fp);
		if(255==ch)
		{
			break;
		}
		unsigned char x=-1;
		fputc(ch,fs);
	}
	fclose(fp);
	fclose(fs);
	return 0;
}

 

 

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

#include <stdio.h>	
#include <string.h>
#include <unistd.h>
#include <stdlib.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 chemical;
	int biology;
}stu;

typedef struct Node
{
	union
	{
		int len;
		stu data;
	};
	struct Node *next;
}linkList,*linkListPtr;

linkListPtr link_create()
{
	linkListPtr H=(linkListPtr)malloc(sizeof(linkList));
	if(NULL==H)
	{
		printf("失败\n");
		return NULL;
	}
	H->len=0;
	H->next=NULL;
	return H;
}

int empty(linkListPtr H)
{
	if(NULL==H)
	{
		return -1;
	}
	return H->len==0;
}

int add(linkListPtr H,stu e)
{
	if(NULL==H)
	{
		return -1;
	}
	linkListPtr p=(linkListPtr)malloc(sizeof(linkList));
	if(NULL==p)
	{
		return -1;
	}
	p->data=e;
	p->next=NULL;
	p->next=H->next;
	H->next=p;
	H->len++;
	return 1;
}

int del(linkListPtr H)
{
	if(NULL==H||empty(H))
	{
		return 0;
	}
	linkListPtr q=H->next;
	H->next=q->next;
	free(q);
	q=NULL;
	H->len--;
	return 1;
}

int main(int argc, const char *argv[])
{
	linkListPtr H=link_create();
	stu s1={"a",1,2,3,4,5,6};
	stu s2={"b",1,2,3,4,5,6};
	stu s3={"c",1,2,3,4,5,6};
	stu s4={"d",1,2,3,4,5,6};
	stu s5={"e",1,2,3,4,5,6};
	add(H,s5);
	add(H,s4);
	add(H,s3);
	add(H,s2);
	add(H,s1);
	FILE* fp=fopen(argv[1],"w");
	if(fp==NULL)
	{
		perror("fopen");
		return 1;
	}
	linkListPtr p=H->next;
	while(p!=NULL)
	{
		fprintf(fp,"%s %d %d %d %d %d %d\n",p->data.name,p->data.chinese,p->data.math,p->data.english,p->data.physics,p->data.chemical,p->data.biology);
		p=p->next;
	}
	fclose(fp);
	while(H->next!=NULL)
	{
		del(H);
	}
	H->len=0;
	FILE* fs=fopen(argv[1],"r");
	if(fs==NULL)
	{
		perror("fopen");
		return 1;
	}
	stu s[5];
	for(int i=0;i<5;i++)
	{
		fscanf(fs,"%s %d %d %d %d %d %d\n",s[i].name,&s[i].chinese,&s[i].math,&s[i].english,&s[i].physics,&s[i].chemical,&s[i].biology);
		if(feof(fs)==1)
		{
			break;
		}
		printf("姓名:%s\n",s[i].name);
		printf("语文:%d\n",s[i].chinese);
		printf("数学:%d\n",s[i].math);
		printf("英语:%d\n",s[i].english);
		printf("物理:%d\n",s[i].physics);
		printf("化学:%d\n",s[i].chemical);
		printf("生物:%d\n",s[i].biology);
		printf("---------------------------\n");
	}
	fclose(fs);
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值