美文网首页
系统 循环写入,读取结构体

系统 循环写入,读取结构体

作者: ie大博 | 来源:发表于2016-12-01 09:08 被阅读0次

    写入结构体

    typedef struct student
    {
        char name[20];
        int num;
        char sex[10];
        int score;
    }student;
    int main(void)
    {
        int fd = -1;
        fd = open("stu.info",O_WRONLY| O_CREAT, S_IRWXU|S_IWGRP|S_IROTH);
        if (-1 == fd)
        {
            printf("open file failed....\n");
            printf("errno = %d\n",errno);
            printf("error:%s\n",strerror(errno));   
        }
        else
        {
            printf("open file ok...\n");
            int ret =-1;
            student  caBuf[3]={{"alex1",100,"boy",81},
                    {"alex2",101,"boy",81},
                    {"alex3",102,"boy",81}};
            int i = 0;
            for(;i<3;i++)
            {
                ret = write(fd,caBuf+i,sizeof(student));    
            }
            
            if(-1 == ret) 
            {
                printf("write error:%s\n",strerror(errno));
            }
            else
            {
                printf("write %d bytes to file \n",ret);
            }
            close(fd);//关闭文件描述符
        }
        return 0;
    }
    

    循环读取结果体

    
    #include <stdio.h>
    #include <unistd.h>  //write()
    #include <errno.h>   //errno
    #include <string.h>  //strerror()
    /*open()*/
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    
    #define PER_WRITE_BYTES 4096
    typedef struct student
    {
        char name[20];
        int num;
        char sex[10];
        int score;
    }student;
    int myOpen(const char *pathname)
    {
        int fd  = -1;
        if (NULL != pathname)
        {
            fd = open(pathname, O_RDONLY | O_CREAT 
                      , S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
            if (-1 == fd)
            {
                printf("open error: %s\n", strerror(errno));
            }
        }
        return fd;
    }
    int main(void)
    {
        int fd = -1;
        fd = myOpen("stu.info");
        if (-1 != fd)
        {
            student stu;
            memset(&stu,'\0',sizeof(student));//初始话里面的内容
            while(1)
            {
                    int ret = -1;
                    ret = read(fd, &stu, sizeof(student));
                    if(ret == 0)
                    {
                            printf("touch the end\n");
                            break;
                    }
                    else if (-1 != ret)
                    {
                            printf("name:%s,num:%d,sex:%s,score:%d\n",
      stu.name,stu.num,stu.sex,stu.score);
                            // printf("%s\n", caBuf);
                    }
                    else
                    {
                            printf("read error:%s\n", strerror(errno));
                            break;
                    }
            }
            close(fd);
        }
    
    
        return 0;
    }
    
    

    相关文章

      网友评论

          本文标题:系统 循环写入,读取结构体

          本文链接:https://www.haomeiwen.com/subject/vabopttx.html