7.15 IO作业

1.实现文件字符拷贝

#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* fp1=fopen(argv[1],"r");
	FILE* fp2=fopen(argv[2],"w");
	if(fp1==NULL)
	{
		perror("fopen1");
		return 1;
	}
	if(fp2==NULL)
	{
		perror("fopen2");
		return 1;
	}
	while(1)
	{
		unsigned char ch=fgetc(fp1);
		if(ch==255){break;}
		fputc(ch,fp2);
	}
	fclose(fp1);
	fclose(fp2);

}

实现效果

 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 a[10];
	int b;
	int c;
	union 
	{
		int len;
		int d;
	};
	struct student *next;
}stu,*stu_p;


int main(int argc, const char *argv[])
{
	//初始化数组
	stu arr[3]={
		{"qq",2,3,4},
		{"ww",6,7,8},
		{"ee",0,11,12}
	};
	FILE* fp1=fopen(argv[1],"w");//写入文件数据
	if(fp1==NULL)
	{
		perror("fopen");
		return 1;
	}
	for(int i=0;i<3;i++)
	{
		fprintf(fp1,"%s %d %d %d\n",arr[i].a,arr[i].b,arr[i].c,arr[i].d);
	}
	fclose(fp1);

	memset(arr,0,sizeof(arr));//清空数组
	for(int i=0;i<3;i++)
	{
		printf("%s %d %d %d\n",arr[i].a,arr[i].b,arr[i].c,arr[i].d);
	}

	//初始化链表
	stu_p H=(stu_p)malloc(sizeof(stu));
	if(H==NULL){
		printf("链表申请失败\n");
		return 1;}
	H->next=NULL;
	H->len=0;


	FILE* fp2=fopen(argv[1],"r");//读取数据到链表
	if(fp2==NULL)
	{
		perror("fopen2");
	}
	stu_p p=H;
	for(int i=0;i<3;i++)
	{
		stu_p new=(stu_p)malloc(sizeof(stu));
		if(new==NULL){
			printf("节点申请失败\n");
			return 1;}
		p->next=new;
		p=new;
		fscanf(fp2,"%s %d %d %d",p->a,&p->b,&p->c,&p->d);
	}
	p=H;
	for(int i=0;i<3;i++)//打印链表数据
	{
		p=p->next;
		printf("%s %d %d %d\n",p->a,p->b,p->c,p->d);
	}
	fclose(fp2);
	return 0;
}

实现效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值