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