1.使用 fputc 和 fgetc 实现文件的拷贝功能
#include <stdio.h>
#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;
//使用fputs和fgets实现文件的拷贝功能
int main(int argc, const char *argv[])
{
//打开具有可读可写权限的两个文件
FILE* rfp=fopen(argv[1],"r");
FILE* wfp=fopen(argv[2],"w");
//读取argv[1]中的所有数据
while(1)
{
int data=fgetc(rfp);
if(EOF==data)
{
break;
}
//写入读取读取的数据到另一个文件
fputc(data,wfp);
}
fclose(rfp);
fclose(wfp);
return 0;
}
2.将结构体数组的加载保存的代码,把结构体数组改成链表再来一次
head.h
#ifndef __HEAD_H__
#define __HEAD_H__
#include <stdio.h>
#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[33];
int ID;
int high;
int heavy;
int score;
}stu,*stu_ptr;
//定义链表结构体
typedef struct node
{
union
{
//len表示学生的个数
int len;
//学生的数据
stu data;
};
struct node *next;
}linklsit,*linklsit_ptr;
//1.创建链表
linklsit_ptr create_node();
//2.链表判空
int empty(linklsit_ptr H);
//3.申请节点封装数据并进行头插
linklsit_ptr create_students_node(linklsit_ptr H,stu e);
//4.遍历
void output(linklsit_ptr H);
//5.头删
int head_del(linklsit_ptr H);
//6.释放链表
void my_free(linklsit_ptr H);
#endif
main.c
#include "head.h"
int main(int argc, const char *argv[])
{
//学生结构体赋值
stu a={"yvhuai",1000,180,120,99};
stu b={"fanqihang",1001,178,120,88};
stu c={"xuchongxin",1002,188,160,77};
stu d={"lihaoran",1003,182,160,66};
stu e={"chenkanghe",1004,178,120,99};
//创建链表
linklsit_ptr H=create_node();
//将数据插入链表
create_students_node(H,a);
create_students_node(H,b);
create_students_node(H,c);
create_students_node(H,d);
create_students_node(H,e);
//遍历
output(H);
//使用fprinf和fscanf函数将链表中的5个学生的信息保存到文件中去,删除其中的信息,然后读取文件中的数据再次显现文件中的信息
//(1)保存数据到文件中
FILE *wfp=fopen(argv[1],"w");
if(wfp==NULL)
{
perror("fopen");
return 1;
}
linklsit_ptr p=H->next;
while(p!=NULL)
{
fprintf(wfp,"%s %d %d %d %d\n",p->data.name,p->data.ID,p->data.high,p->data.heavy,p->data.score);
p=p->next;
}
fclose(wfp);
//(2)清空链表
my_free(H);
//(3)读取文件内容
FILE *rfp=fopen(argv[1],"r");
if(rfp==NULL)
{
perror("fopen");
return 1;
}
stu s[5];
for(int i=0;i<5;i++)
{
fscanf(rfp,"%s %d %d %d %d\n",s[i].name,&s[i].ID,&s[i].high,&s[i].heavy,&s[i].score);
if(feof(rfp)==1)
{
break;
}
}
//显示
for(int i=0;i<5;i++)
{
printf("姓名:%s\n",s[i].name);
printf("ID:%d\n",s[i].ID);
printf("身高:%d\n",s[i].high);
printf("体重:%d\n",s[i].heavy);
printf("得分:%d\n",s[i].score);
printf("---------\n");
}
fclose(rfp);
return 0;
}
fun.c
#include "head.h"
//1.创建链表
linklsit_ptr create_node()
{
//申请节点大小空间
linklsit_ptr H=(linklsit_ptr)malloc(sizeof(linklsit));
if(NULL==H)
{
printf("创建链表失败\n");
return NULL;
}
H->len=0;
H->next=NULL;
printf("链表创建成功\n");
return H;
}
//2.链表判空
int empty(linklsit_ptr H)
{
if(NULL==H)
{
printf("判断为空失败\n");
return -1;
}
return H->len==0;
}
//3.申请节点封装数据并进行头插
linklsit_ptr create_students_node(linklsit_ptr H,stu e)
{
//申请节点大小空间
linklsit_ptr P=(linklsit_ptr)malloc(sizeof(linklsit));
if(NULL==P)
{
printf("申请节点失败\n");
return NULL;
}
P->data=e;
P->next=NULL;
//定义移动的指针
linklsit_ptr q=H;
P->next=q->next;
q->next=P;
H->len++;
return H;
}
//4.遍历
void output(linklsit_ptr H)
{
if(NULL==H || empty(H))
{
printf("遍历失败\n");
return;
}
linklsit_ptr p=H;
while(p->next!=NULL)
{
p=p->next;
printf("姓名:%s ID:%d 身高:%d 体重:%d 得分:%d\n",p->data.name,p->data.ID,p->data.high,p->data.heavy,p->data.score);
}
printf("------------------------\n");
}
//5.头删
int head_del(linklsit_ptr H)
{
//判断所接受的链表是否合法
//判空
if(NULL == H || empty(H))
{
printf("删除失败!\n");
return 0;
}
//定义一个指针指向头结点后的第一个节点
linklsit_ptr q = H->next;
//删除
H->next = q->next; //H->next = H->next->next;
//释放空间
free(q);
q=NULL; //避免野指针
//成功删除 链表长度自减
H->len--;
return 1;
}
//6.释放链表
void my_free(linklsit_ptr H)
{
if(NULL == H || empty(H))
{
printf("释放失败!\n");
return ;
}
while(H->next!=NULL)
{
head_del(H);
}
H=NULL;
printf("释放成功\n");
}