7.15作业

1.使用 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* rfp=fopen(argv[1],"r");
	FILE* wfp=fopen(argv[2],"w");
	if(rfp==NULL)
	{
		perror("rfopen");
		return 1;
	}
	if(wfp==NULL)
	{
		perror("wfopen");
		return 1;
	}
	while(1)
	{
		char ch=fgetc(rfp);
		if(feof(rfp)==1)break;
		fputc(ch,wfp);
	}
	fclose(rfp);
	fclose(wfp);
	return 0;
}

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

#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;

struct Student{
	char name[20];
	int chinise;
	int math;
	int english;
	int physical;
	int chemical;
	int biology;
};
typedef struct Student dataType;
typedef struct node
{
	union
	{
		int len;
		dataType data;
	};
	struct node *next;
}linkList,*linkListPtr;
linkListPtr create()
{
	linkListPtr H=(linkListPtr)malloc(sizeof(linkList));
	if(NULL==H)
	{
		perror("create");
		return NULL;
	}
	H->len=0;
	H->next=NULL;
	return H;
}
int add_head(linkListPtr H,dataType e)
{
	linkListPtr node=(linkListPtr)malloc(sizeof(linkList));
	if(NULL==node)
	{
		perror("add_init");
		return 0;
	}
	node->data=e;
	node->next=H->next;
	H->next=node;
	H->len++;
	return 1;
}
int add_tail(linkListPtr H,dataType e)
{
	linkListPtr node=(linkListPtr)malloc(sizeof(linkList));
	if(NULL==node)
	{
		perror("add_init");
		return 0;
	}
	linkListPtr p=H; 		
	while(p->next!=NULL){p=p->next;}
	node->data=e;
	node->next=p->next;
	p->next=node;
	H->len++;
	return 1;
}
void show(linkListPtr H)
{
	if(NULL==H)
	{
		perror("out");
		return;
	}
	linkListPtr p=H;
	for(int i=0;i<H->len;i++)
	{
		p=p->next;
		printf("%s %d %d %d %d %d %d\n",p->data.name,p->data.chinise,p->data.math,p->data.english,p->data.physical,p->data.chemical,p->data.biology);
	}
}
int main(int argc, const char *argv[])
{
	FILE *fp=fopen(argv[1],"w");
	if(fp==NULL)
	{
		perror("fopen");
		return 1;
	}
	linkListPtr H=create();
	dataType e1={"ab",0,0,0,0,0,0};
	add_head(H,e1);
	dataType e2={"cd",1,1,1,1,1,1};
	add_head(H,e2);
	dataType e3={"ef",2,2,2,2,2,2};
	add_head(H,e3);
	dataType e4={"gh",3,3,3,3,3,3};
	add_head(H,e4);
	dataType e5={"ij",4,4,4,4,4,4};
	add_head(H,e5);
	linkListPtr p=H;
	for(int i=0;i<H->len;i++)
	{
		p=p->next;
		fprintf(fp,"%s %d %d %d %d %d %d\n",p->data.name,p->data.chinise,p->data.math,p->data.english,p->data.physical,p->data.chemical,p->data.biology);
	}
	show(H); 
	fclose(fp);
	bzero(H,sizeof(H));
	fp=fopen(argv[1],"r");
	while(1)
	{
		dataType e;
		fscanf(fp,"%s %d %d %d %d %d %d\n",e.name,&e.chinise,&e.math,&e.english,&e.physical,&e.chemical,&e.biology);
		add_tail(H,e);
		if(feof(fp)==1){break;}
	}
	show(H); 
	fclose(fp);
	return 0;
}

相关推荐

  1. 作业..........

    2024-07-15 22:20:03       51 阅读
  2. IOS面试题编程机制 71-75

    2024-07-15 22:20:03       31 阅读
  3. 785. 快速排序

    2024-07-15 22:20:03       54 阅读
  4. leetcode705-Design HashSet

    2024-07-15 22:20:03       186 阅读
  5. 735. 小行星碰撞

    2024-07-15 22:20:03       31 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-15 22:20:03       70 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-15 22:20:03       74 阅读
  3. 在Django里面运行非项目文件

    2024-07-15 22:20:03       62 阅读
  4. Python语言-面向对象

    2024-07-15 22:20:03       72 阅读

热门阅读

  1. 【C++】继承与多态相关11道面试题整理

    2024-07-15 22:20:03       21 阅读
  2. .NET Core项目中添加MIME类型

    2024-07-15 22:20:03       22 阅读
  3. 对于RBAC模型的认识

    2024-07-15 22:20:03       22 阅读
  4. 开源项目面临的机遇与挑战

    2024-07-15 22:20:03       21 阅读
  5. 【C++语言】正则表达式

    2024-07-15 22:20:03       20 阅读
  6. Mybatis防止SQL注入

    2024-07-15 22:20:03       22 阅读
  7. Vue2中的指令修饰符

    2024-07-15 22:20:03       20 阅读
  8. Python面试题:如何在 Python 中处理大数据集?

    2024-07-15 22:20:03       23 阅读