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* fp = fopen(argv[1],"r");
if(fp==NULL)
{
perror("fopen1:");
return 1;
}
FILE* wfp=fopen(argv[2],"w");
if(wfp==NULL)
{
perror("fopen2:");
return 1;
}
while(1)
{
int ch = fgetc(fp);
if(ch == EOF)
{
break;
}
fputc(ch,wfp);
}
fclose(fp);
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>
#define MAX 30
typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;
typedef int DataType;
typedef struct student
{
char name[MAX];
int chinese;
int english;
int math;
}STU,*stu;
typedef struct stu
{
union {
int len;
STU s[5];
};
struct stu* next;
}stud,*stu_ptr;
//创建单链表
stu_ptr create()
{
stu_ptr H = (stu_ptr)malloc(sizeof(stud));
if(H==NULL)
{
printf("链表创建失败\n");
return NULL;
}
H->len = 0;
H->next =NULL;
printf("链表创建成功\n");
return H;
}
//申请节点 存放数据
stu_ptr create_node(char name[],int chinese,int math,int english)
{
stu_ptr p = (stu_ptr)malloc(sizeof(stud));
if(p==NULL)
{
printf("申请失败\n");
return NULL;
}
strcpy(p->s->name,name);
p->s->chinese = chinese;
p->s->english = english;
p->s->math = math;
p->next = NULL;
return p;
}
//链表判空
int empty(stu_ptr H)
{
if(H==NULL)
{
printf("判空失败\n");
return -1;
}
return H->len == 0;
}
//链表判满
int fill(stu_ptr H)
{
if(H==NULL)
{
printf("判满失败\n");
return -1;
}
return H->len==5;
}
//头插
int head_add(stu_ptr H,char name[],int chinese,int english,int math)
{
if(H==NULL || fill(H))
{
printf("插入失败\n");
return 0;
}
stu_ptr p =create_node(name,chinese,english,math);
p->next=H->next;
H->next=p;
H->len++;
return 1;
}
//尾删
int tail_del(stu_ptr H)
{
if(H==NULL || empty(H))
{
printf("尾删失败\n");
return 0;
}
stu_ptr q = H;
for(int i=0;i<H->len-1;i++)
{
q=q->next;
}
stu_ptr p = q->next;
q->next = p->next;
free(p);
p=NULL;
H->len--;
return 1;
}
//遍历链表
void show(stu_ptr H)
{
if(H==NULL||empty(H))
{
printf("无效遍历\n");
return;
}
stu_ptr p =H;
for(int i=0;i<H->len;i++)
{
p=p->next;
printf("姓名:%s 语文:%d 英语:%d 数学:%d\n",p->s->name,p->s->chinese,p->s->english,p->s->math);
}
return;
}
//释放链表
int free_ptr(stu_ptr H)
{
if(H==NULL)
{
printf("释放失败\n");
return 0;
}
while(H->next!=NULL)
{
tail_del(H);
}
free(H);
H=NULL;
printf("链表释放成功\n");
return 1;
}
int main(int argc, const char *argv[])
{
//创建链表
stu_ptr H = create();
//头插
head_add(H,"张三",100,90,97);
head_add(H,"李四",99,94,93);
head_add(H,"王五",80,90,87);
head_add(H,"赵四",90,70,87);
head_add(H,"郑六",60,90,97);
//遍历链表
show(H);
FILE * wfp = fopen(argv[1],"w");
if(wfp == NULL)
{
perror("fopen1:");
return 1;
}
stu_ptr q =H;
while(q->next!=NULL)
{
q=q->next;
fprintf(wfp,"%s %d %d %d\n",q->s->name,q->s->chinese,q->s->english,q->s->math);
}
fclose(wfp);
while(H->next!=NULL)
{
tail_del(H);
}
FILE *rfp = fopen(argv[1],"r");
if(rfp == NULL)
{
perror("fopen2:");
return 1;
}
int i=0;
while(1)
{
STU s[5]={0};
fscanf(rfp,"%s %d %d %d",s[i].name,&s[i].chinese,&s[i].english,&s[i].math);
head_add(H,s[i].name,s[i].chinese,s[i].english,s[i].math);
if(feof(rfp)==1)
{
break;
}
i++;
}
show(H);
fclose(rfp);
free_ptr(H);
H=NULL;
return 0;
}