Linux下open()、read()、write()函数应用实例-------链表的储存与读取

  1 #include <sys/types.h>
  2 #include <sys/stat.h>
  3 #include <fcntl.h>
  4 #include<stdio.h>
  5 #include<unistd.h>
  6 #include<stdlib.h>
  7 #include<string.h>
  8 typedef struct students
  9 {
 10     int num;
 11     char name[20];
 12     struct students *next;
 13 }Stu,*PStu;
 14 PStu head=NULL;
 15 PStu r;
 16 //-----写入链表-----
 17 void creat_list(PStu *phead,PStu r)// 需要把读取到的最后一个节点指针传参进来
 18 {
 19     PStu ptr;
 20     char choose;
 21     int num;
 22     char name[20];
 23     while(1)
 24     {
 25         printf("do you want to add a student?(y or else)\n");
 26         scanf("%c",&choose);getchar();
 27         if(choose=='y')
 28         {
 29             ptr=malloc(sizeof(Stu));
 30             printf("id=");
 31             scanf("%d",&num);getchar();
 32             printf("name=");
 33             scanf("%s",name);getchar();
 34             ptr->num=num;
 35             strcpy(ptr->name,name);
 36             ptr->next=NULL;
 37             if(*phead==NULL)//如果没有读取到,则从头开始创建
 38             {
 39                 *phead=ptr;
 40  
 41             }
 42             else
 43                 r->next=ptr;
 44             r=ptr;
 45         }
 46  
 47         else if(choose!='y')
 48             break;
 49     }
 50 }
 51 //-----打印函数-----
 52 void printf_list(PStu ptr)
 53 {
 54     while(ptr!=NULL)//无头节点
 55     {
 56         printf("%d\t%s\n",ptr->num,ptr->name);
 57         ptr=ptr->next;
 58     }
 59 }
 60 //-------读文件------
 61 PStu read_file(PStu *head)
 62 {
 63     int fd;
 64     int ret;
 65     PStu p,r;
 66     fd=open("./stu.txt",O_CREAT|O_RDONLY,00700);
 67     if(fd<0)
 68     {
 69         perror("open");exit(0);
 70     }
 71     else
 72     {
 73         while(1)
 74         {
 75             p=malloc(sizeof(Stu));
 76             ret=read(fd,p,sizeof(Stu));//从文件中读取内容存在(节点)指针中
 77             if(ret<0)
 78             {
 79                 perror("read");break;
 80             }
 81             if(ret==0)//read返回值为0时结束
 82             {
 83                 printf("-------read over------\n");
 84                 break;
 85             }
 86             if(ret>0)//read返回值>0时循环读取并往后移动
 87             {
 88                 p->next=NULL;
 89                 if(*head==NULL)
 90                 {
 91                     *head=p;
 92                 }
 93                 else
 94                     r->next=p;
 95                 r=p;
 96             }
 97         }
 98     }
 99     close(fd);return r;//返回读取到的最后一个节点指针
100 }
101 //-----写文件------
102 void write_file(PStu p)
103 {
104     /*  read_file(PStu *ptr);*/
105     int fd;
106     int ret;
107     fd=open("./stu.txt",O_WRONLY);
108     if(fd<0)
109     {
110         perror("open");
111         return ;
112     }
113     for(;p!=NULL;p=p->next)
114     {
115         ret=write(fd,p,sizeof(Stu));
116         if(ret<0)
117             perror("write");
118     }
119     close(fd);
120 }
121 //------主函数-----
122 int main()
123 {
124     r=read_file(&head);//得到所读取到的最后一个节点指针
125     PStu p;
126     creat_list(&head,r);
127     write_file(head);
128     if(head==NULL)
129         printf("无学生\n");
130     else
131     {
132         printf("学号\t姓名\n");
133         printf_list(head);
134     }
135     return 0;
136 }

 

posted on 2018-06-27 09:59  IT8343  阅读(1031)  评论(0)    收藏  举报