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