导图
半成品的程序save
#include <stdio.h>
#include <string.h>
#include <stdlib.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[20];
int chinese;//语文成绩
int math;
int english;
int physical;
int chemical;
int biological;
}stu;
typedef struct node
{
union
{
int len;
stu pupil[5];
};
struct node *next;
}link1,*linkPtr;
//创建
linkPtr create()
{
linkPtr L=(linkPtr)malloc(sizeof(link1));
if(NULL==L)
{
printf("创建失败\n");
return NULL;
}
L->len=0;
memset(L->pupil,0,sizeof(L->pupil));
L->next=NULL;
printf("创建成功\n");
return L;
}
//封装数据
linkPtr create_add(stu e,int i)
{
linkPtr P=(linkPtr)malloc(sizeof(link1));
if(NULL==P)
{
printf("创建失败\n");
return NULL;
}
strcpy(P->pupil[P->len].name,e.name);
P->pupil[i].chinese=e.chinese;
P->pupil[i].math=e.math;
P->pupil[i].english=e.english;
P->pupil[i].physical=e.physical;
P->pupil[i].chemical=e.chemical;
P->pupil[i].biological=e.biological;
P->next=NULL;
return P;
}
//头插
int add(linkPtr L,stu e)
{
if(NULL==L)
{
printf("创建失败\n");
return -1;
}
linkPtr P=create_add(e,L->len);
P->next=L->next;
L->next=P;
L->len++;
return 1;
}
//遍历
void show(linkPtr L)
{
if(NULL==L)
{
printf("创建失败\n");
return ;
}
linkPtr P=L;
for(int i=0;i<L->len;i++)
{
printf("%s ",P->pupil[i].name);
printf("%d ",P->pupil[i].chinese);
printf("%d ",P->pupil[i].math);
printf("%d ",P->pupil[i].english);
printf("%d ",P->pupil[i].physical);
printf("%d ",P->pupil[i].chemical);
printf("%d ",P->pupil[i].biological);
printf("\n");
P=P->next;
}
}
int main(int argc, const char *argv[])
{
linkPtr L=create();
stu a0={"张三",33,34,35,36,37,38};
stu a1={"李四",43,44,45,46,47,48};
stu a2={"王五",53,54,55,56,57,58};
add(L,a0);
add(L,a1);
add(L,a2);
show(L);
FILE* fp=fopen("./text1.txt","w");
if(fp==NULL)
{
perror("fopen");
return 1;
}
linkPtr P=L;
for(int i=0;i<3;i++)
{
fprintf(fp,"%s %d %d %d %d %d %d \n",
P->pupil[i].name,P->pupil[i].chinese,P->pupil[i].math,
P->pupil[i].english,P->pupil[i].physical,
P->pupil[i].chemical,P->pupil[i].biological);
P=P->next;
}
fclose(fp);
return 0;
}
load跟练习一样,不用动
#include <stdio.h>
#include <string.h>
#include <stdlib.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[20];
int chinese;//语文成绩
int math;
int english;
int physical;
int chemical;
int biological;
}stu_t;
int main(int argc, const char *argv[])
{
FILE* lfp=fopen("./text1.txt","r");
stu_t a[3]={0};
int i=0;
if(NULL==lfp)
{
perror("fopen");
return 1;
}
while(1)
{
int A=fscanf(lfp,"%s %d %d %d %d %d %d \n"
,a[i].name,&a[i].chinese,&a[i].math,&a[i].english,&a[i].physical,&a[i].chemical,&a[i].biological);
//int A=fscanf(lfp,"%d",);
if(EOF==A)
break;
i++;
//fscanf(lfp,"%d ",a[i++]);
}
fclose(lfp);
for(int j=0;j<i;j++)
{
printf("%s\n",a[j].name);
printf("%d\n",a[j].chinese);
printf("%d\n",a[j].math);
printf("%d\n",a[j].english);
printf("%d\n",a[j].physical);
printf("%d\n",a[j].chemical);
printf("%d\n",a[j].biological);
printf("\n");
}
return 0;
}