系统编程-文件操作

系统调用

  • 系统调用主要进行进程控制(系统调用是系统提供用户的一个接口)
  1. flag:O_RDONLY,O_WRONLY,O_RDWR
  2. 路径:
  • 绝对路径:以/开始的路径称之为绝对路径/user/inclue
  • 相对路径:以./开始的路径称之为相对路径,./可以省略不写./..1612-->../1612
  1. 使用flag指定的方式打开指定路径的文件
  2. 若文件打开成功,返回该文件的一个新的文件描述符
  3. 文件描述符用于文件操作
  4. 若文件打开失败,返回-1
  5. pathname:"./stu.info"
  • 一个文件的创建,以及文件的属性

//1_read_only.c

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<stdio.h>
#include<errno.h>//错误号erron
#include<string.h>//strerror()
#include<unistd.h>
//flag:O_RDONLY,O_WRONLY,O_RDWR
//路径:
//绝对路径:以/开始的路径称之为绝对路径
//相对路径:以./开始的路径称之为相对路径,./可以省略不写
//使用flag指定的方式打开指定路径的文件
//若文件打开成功,返回该文件的一个新的文件描述符
//文件描述符用于文件操作
//若文件打开失败,返回-1
//int open(const char *pathname,int flag);
int main()
{
    int fd = -1;
    //以只读方式打开文件,若文件不存在,不会自动创建文件,打开失败
    fd=open("stu.info",O_RDONLY);
    if(-1==fd)
    {
        
        printf("open file failed...\n");
        //当打开失败,会对errno设置错误值
        //该错误值代表发生的错误
        printf("errno=%d\n",errno);
        //将strerror获得erron代表的错误信息
        printf("erron:%s\n",strerror(errno));
    }
    else
    {
        printf("open file ok...\n");
        //关闭打开的文件描述符:关闭文件
        close(fd);
    }
    return 0;
}

//1_write_only.c


#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<stdio.h>
#include<errno.h>//错误号erron
#include<string.h>//strerror()
#include<unistd.h>
//flag:O_RDONLY,O_WRONLY,O_RDWR
//路径:
//绝对路径:以/开始的路径称之为绝对路径
//相对路径:以./开始的路径称之为相对路径,./可以省略不写
//使用flag指定的方式打开指定路径的文件
//若文件打开成功,返回该文件的一个新的文件描述符
//文件描述符用于文件操作
//若文件打开失败,返回-1
//int open(const char *pathname,int flag);
int main()
{
    int fd = -1;
    //以只读方式打开文件,若文件不存在,不会自动创建文件,打开失败
    fd=open("stu.info",O_RDWR);
    if(-1==fd)
    {
        
        printf("open file failed...\n");
        //当打开失败,会对errno设置错误值
        //该错误值代表发生的错误
        printf("errno=%d\n",errno);
        //将strerror获得erron代表的错误信息
        printf("erron:%s\n",strerror(errno));
        
    }
    else
    {
        printf("open file ok...\n");
        //关闭打开的文件描述符:关闭文件
        close(fd);
    }

    return 0;
}

//create file.c


#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<stdio.h>
#include<errno.h>//错误号erron
#include<string.h>//strerror()
#include<unistd.h>
//flag:O_RDONLY,O_WRONLY,O_RDWR
//路径:
//绝对路径:以/开始的路径称之为绝对路径
//相对路径:以./开始的路径称之为相对路径,./可以省略不写
//使用flag指定的方式打开指定路径的文件
//若文件打开成功,返回该文件的一个新的文件描述符
//文件描述符用于文件操作
//若文件打开失败,返回-1
//int open(const char *pathname,int flag);
//mode:
//   S_IRWXU:用户对文件具有读,写,执行的权限
//   S_IRUSR:用户对文件具有读的权限
//       S_IWUSR:用户对文件具有写的权限
//       S_IXUSR:用户对文件具有执行的权限 

//   S_IRWXG:用户对文件具有读,写,执行的权限
//   S_IRGRP:用户对文件具有读的权限
//       S_IWGRP:用户对文件具有写的权限
//       S_IXGRP:用户对文件具有执行的权限 

//   S_IRWXO:用户对文件具有读,写,执行的权限
//   S_IROTH:用户对文件具有读的权限
//       S_IWOTH:用户对文件具有写的权限
//       S_IXOTH:用户对文件具有执行的权限 

//当需要指定多个权限的时候,
//使用mode指定的文件权限的方式在指定的路径下创建文件
//int create (const char *pathname,mode_t mode);
int main()
{
    int fd = -1;
    //以只读方式打开文件,若文件不存在,不会自动创建文件,打开失败
    fd=open("stu.info",O_RDONLY);
    if(-1==fd)
    {
        
        printf("open file failed...\n");
        //当打开失败,会对errno设置错误值
        //该错误值代表发生的错误
        printf("errno=%d\n",errno);
        //将strerror获得erron代表的错误信息
        printf("erron:%s\n",strerror(errno));
        if(2==errno)
        {
            //以用户具有读写权限,用户组具有读权限
                        //其他用户具有读权限的方式创建文件
                        //相当于创建一个文件,并且以写的方式打开该文件
                        //并且将该文件内容截断(清空文件)
            fd=creat("stu.info",s_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
            if(-1==fd)
            {
                printf("erron=%s\n",strerror(errno));
            }
            else
            {
                printf("create file ok\n");
            }
        }
    }
    else
    {
        printf("open file ok...\n");
        //关闭打开的文件描述符:关闭文件
        close(fd);
    }
    return 0;
}


//2create_file.c

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<stdio.h>
#include<errno.h>//错误号erron
#include<string.h>//strerror()
#include<unistd.h>
//flag:O_RDONLY,O_WRONLY,O_RDWR
//路径:
//绝对路径:以/开始的路径称之为绝对路径
//相对路径:以./开始的路径称之为相对路径,./可以省略不写
//使用flag指定的方式打开指定路径的文件
//若文件打开成功,返回该文件的一个新的文件描述符
//文件描述符用于文件操作
//若文件打开失败,返回-1
//int open(const char *pathname,int flag);
//mode:
//   S_IRWXU:用户对文件具有读,写,执行的权限
//   S_IRUSR:用户对文件具有读的权限
//       S_IWUSR:用户对文件具有写的权限
//       S_IXUSR:用户对文件具有执行的权限 

//   S_IRWXG:用户对文件具有读,写,执行的权限
//   S_IRGRP:用户对文件具有读的权限
//       S_IWGRP:用户对文件具有写的权限
//       S_IXGRP:用户对文件具有执行的权限 

//   S_IRWXO:用户对文件具有读,写,执行的权限
//   S_IROTH:用户对文件具有读的权限
//       S_IWOTH:用户对文件具有写的权限
//       S_IXOTH:用户对文件具有执行的权限 

//当需要指定多个不同权限时,使用“|“连接: S_IRWXU | S_IRGRP
//使用mode指定的文件权限的方式在指定的路径下创建文件
//返回该文件新的文件描述符
//int creat(const char *pathname, mode_t mode);
int myOpen(const char *pathname)
{
    
    int fd = -1;
    //以只读方式打开文件,若文件不存在,不会自动创建文件,打开失败
    fd=open("stu.info",O_RDONLY);
    if(-1==fd)
    {
        
                if(2==errno)
        {
            //以用户具有读写权限,用户组具有读权限
                        //其他用户具有读权限的方式创建文件
                        //相当于创建一个文件,并且以写的方式打开该文件
                        //并且将该文件内容截断(清空文件)
            fd=creat("stu.info",S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
            if(-1==fd)
            {
                printf("erron=%s\n",strerror(errno));
                return 0;
            }
            else
            {
                printf("create file ok\n");
            }
        }
    }
    else
    {
        printf("erron=%s\n",strerror(errno));
    }
    return fd;
}
int main()
{
    int fd=-1;
    fd=myOpen("stu.info");
    if(-1!=fd)
    {
        printf("open file ok...\n");
        close(fd);
    }
    return 0;
}

//1_create.c

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<stdio.h>
#include<errno.h>//错误号erron
#include<string.h>//strerror()
#include<unistd.h>
//flag:O_RDONLY,O_WRONLY,O_RDWR
//路径:
//绝对路径:以/开始的路径称之为绝对路径
//相对路径:以./开始的路径称之为相对路径,./可以省略不写
//使用flag指定的方式打开指定路径的文件
//若文件打开成功,返回该文件的一个新的文件描述符
//文件描述符用于文件操作
//若文件打开失败,返回-1
//int open(const char *pathname,int flag);
int main()
{
    int fd = -1;
    //以只读方式打开文件,若文件不存在,不会自动创建文件,打开失败
    //O_CREAT | O_EXCL:
    //  若文件存在,则创建失败,并且打开文件失败
    //  若文件不存在,则创建文件,然后按照指定的打开方式打开文件
    //fd=open("stu.info",O_RDONLY | O_CREAT | O_EXCL,S_IRWXU | S_IWGRP | S_IROTH);


    //O_CREAT:
    //若文件不存在,则按照指定的权限创建文件,并且打开文件。然后按照指定的打开方式打开文件
    //若文件存在,则按照指定的打开方式打开文件
    fd=open("stu.info",O_RDONLY | O_CREAT,S_IRWXU | S_IWGRP | S_IROTH);
    if(-1==fd)
    {
        
        printf("open file failed...\n");
        //当打开失败,会对errno设置错误值
        //该错误值代表发生的错误
        printf("errno=%d\n",errno);
        //将strerror获得erron代表的错误信息
        printf("erron:%s\n",strerror(errno));
    }
    else
    {
        printf("open file ok...\n");
        //关闭打开的文件描述符:关闭文件
        close(fd);
    }
    return 0;
}


//write.c

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<stdio.h>
#include<errno.h>//错误号erron
#include<string.h>//strerror()
#include<unistd.h>
//flag:O_RDONLY,O_WRONLY,O_RDWR
//路径:
//绝对路径:以/开始的路径称之为绝对路径
//相对路径:以./开始的路径称之为相对路径,./可以省略不写
//使用flag指定的方式打开指定路径的文件
//若文件打开成功,返回该文件的一个新的文件描述符
//文件描述符用于文件操作
//若文件打开失败,返回-1
//int open(const char *pathname,int flag);
int main()
{
    int fd = -1;
    //以只读方式打开文件,若文件不存在,不会自动创建文件,打开失败
    //O_CREAT | O_EXCL:
    //  若文件存在,则创建失败,并且打开文件失败
    //  若文件不存在,则创建文件,然后按照指定的打开方式打开文件
    //fd=open("stu.info",O_RDONLY | O_CREAT | O_EXCL,S_IRWXU | S_IWGRP | S_IROTH);


    //O_CREAT:
    //若文件不存在,则按照指定的权限创建文件,并且打开文件。然后按照指定的打开方式打开文件
    //若文件存在,则按照指定的打开方式打开文件
    fd=open("stu.info",O_WRONLY | O_CREAT,S_IRWXU | S_IWGRP | S_IROTH);
    if(-1==fd)
    {
        
        printf("open file failed...\n");
        //当打开失败,会对errno设置错误值
        //该错误值代表发生的错误
        printf("errno=%d\n",errno);
        //将strerror获得erron代表的错误信息
        printf("erron:%s\n",strerror(errno));
    }
    else
    {
        printf("open file ok...\n");
        int ret=-1;
        char caBuf[32]="hello world";
        ret=write(fd,caBuf,strlen(caBuf));
        if(-1==ret)
        {
            printf("write erron:%s\n",strerror(errno));
        }
        else
        {
            printf("write %d bytes to file\n",ret);
        }
        //关闭打开的文件描述符:关闭文件
        close(fd);
    }
    return 0;
}


  • 大文件的读写

//write_big_data

#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
#define NAME_LEN 32
int myOpen(const char *pathname)
{
    int fd  = -1;
    if (NULL != pathname)
    {
        fd = open(pathname, O_WRONLY | O_CREAT
                  , S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
        if (-1 == fd)
        {
            printf("open error: %s\n", strerror(errno));
        }
    }
    return fd;
}
int myWrite(int fd, char *pData, int iTotalSize)
{
    //对形参的值进行有效性检查
    if (-1 != fd && NULL != pData && iTotalSize > 0)
    {
        int i = 0;
        //若数据量比较大是,write可能一次性写不玩
        //这种情况下,必须分多次循环的去写

        //剩余要写的字节数
        int iLeft = iTotalSize;
        
        //已写入的字节数
        int iWirted = 0;
        
        //写数据时的返回值,出错返回-1,
        //成功返回实际写入的字节数
        int ret = -1;

        //如果剩余的数据量大于等于PER_WRITE_BYTES
        //则指定该次写入文件的数据量为PER_WRITE_BYTES
        //否则指定为剩余的数据量:iLeft
        if (iLeft >= PER_WRITE_BYTES)
        {
            ret = write(fd, pData, PER_WRITE_BYTES);
        }
        else
        {
            ret = write(fd, pData, iLeft);
        }
        if (-1 == ret)
        {
            printf("write error: %s\n", strerror(errno));
        }
        else
        {
            printf("%d, write %d bytes to file\n", ++i, ret);
            //剩余数据量减去实际写入的数据量
            //得到新的剩余数据量
            iLeft -= ret;

            //已写的数据量加上实际写入的数据量
            //得到新的已写数据量
            iWirted += ret;
            //如果上次写入没有出错并且还有数据没写完
            //则循环接着写
            while (ret && iLeft)
            {
                if (iLeft >= PER_WRITE_BYTES)
                {
                    //指针往后偏移到未写的数据位置
                    //从该位置开始将数据写入文件
                    ret = write(fd, pData+iWirted
                                , PER_WRITE_BYTES);
                }
                else
                {
                    ret = write(fd, pData+iWirted
                                , iLeft);
                }
                if (-1 != ret)
                {
                    iLeft -= ret;
                    iWirted += ret;
                    printf("%d, write %d bytes to file\n"
                           , ++i, ret);
                }
                else
                {
                    printf("write error: %s\n", strerror(errno));
                }
            }
        }
    }
}

int main(void)
{
    int fd = -1;
    fd = myOpen("test.info");
    if (-1 != fd)
    {
        char caBuf[4096789] = {'A'};
        myWrite(fd, caBuf, sizeof(caBuf));
        
        close(fd);
    }   

    return 0;
}



  • 大文件的读
#include <stdio.h>
#include <unistd.h>  //write()  read() sleep()
#include <errno.h>   //errno
#include <string.h>  //strerror()
/*open()*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define PER_IO_BYTES 4096

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;
}
#if 1
int myRead(int fd, char *pData, int iTotalSize)
{
    //对形参的值进行有效性检查
    if (-1 != fd && NULL != pData && iTotalSize > 0)
    {
        int i = 0;
        //若要读的数据量比较大时,read可能一次性读不完
        //这种情况下,必须分多次循环的去读

        //剩余要读的字节数
        int iLeft = iTotalSize;
        
        //已读取的字节数
        int iReaded = 0;
        
        //读数据时的返回值,出错返回-1,
        //成功返回实际读取的字节数
        int ret = -1;

        //如果剩余的数据量大于等于PER_IO_BYTES
        //则指定该次读取文件的数据量为PER_IO_BYTES
        //否则指定为剩余的数据量:iLeft
        if (iLeft >= PER_IO_BYTES)
        {
            ret = read(fd, pData, PER_IO_BYTES);
        }
        else
        {
            ret = read(fd, pData, iLeft);
        }
        if (-1 == ret)
        {
            printf("read error: %s\n", strerror(errno));
        }
        else
        {
            printf("%d, read %d bytes from file\n", ++i, ret);
        //  sleep(1);
            //剩余数据量减去实际读取的数据量
            //得到新的剩余数据量
            iLeft -= ret;

            //已读取的数据量加上实际读取的数据量
            //得到新的已读取数据量
            iReaded += ret;
            //如果上次读取没有出错并且还有数据没读取完
            //则循环接着读
            while (ret && iLeft)
            {
                if (iLeft >= PER_IO_BYTES)
                {
                    ret = read(fd, pData+iReaded
                                , PER_IO_BYTES);
                }
                else
                {
                    ret = read(fd, pData+iReaded
                                , iLeft);
                }
                if (-1 != ret)
                {
                    iLeft -= ret;
                    iReaded += ret;
                    printf("%d, read %d bytes from file, left: %d, iReaded:%d\n"
                           , ++i, ret, iLeft, iReaded);
        //          sleep(1);
                }
                else
                {
                    printf("write error: %s\n", strerror(errno));
                }
            }
        }
    }
}
#endif

//fd:要进行读操作的文件的文件描述符
//buf:存放读的数据的空间首地址
//count:指定本次要从文件中读取多少个字节
//read出错返回-1,成功返回实际读取的字节数
//ssize_t read(int fd, void *buf, size_t count);

int main(void)
{
    int fd = -1;
    fd = myOpen("test.data");
    if (-1 != fd)
    {
        char caBuf[1000100] = {'\0'};
        myRead(fd, caBuf, sizeof(caBuf));
//      caBuf[4096789-1] = '\0';
//      printf("%s\n", caBuf);
        close(fd);
    }   

    return 0;
}

  • 学生信息的读写

#include <stdio.h>
#include <errno.h>//errno
#include <string.h>//strerror()
#include <unistd.h>
/*open()*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
typedef struct student
{
        int id;
        char name[10];
        char sex[5];
        float score;
}student;
int main(void)
{
        int fd=-1;
        fd=open("stuWrite.info",O_WRONLY|O_CREAT,S_IRWXU|S_IWGRP|S_IROTH);//pathname:"./stu.info"
        if(fd==-1)
        {
                printf("open file failed....\n");
                printf("error:%s\n",strerror(errno));
        }
        else
        {
                printf("open file ok....\n");
                student stu1={1,"zhangsan","boy",95.5};
                int ret;
                ret=write(fd,&stu1,sizeof(student));
                if(ret==-1)
                {
                    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
#define NAME_LEN 32
typedef struct Student
{
    int id;
    char name[10];
    char sex[5];
    float 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("stuWrite.info");
    if (-1 != fd)
    {
        student stu;
        memset(&stu,'\0',sizeof(student));
        int ret=-1;
        ret=read(fd,&stu,sizeof(student));
        if(ret!=-1)
        {
            printf("id=%d name=%s sex=%s score=%f\n",stu.id,stu.name,stu.sex,stu.score);
        }
        else
        {
            printf("read error:%s\n",strerror(errno));      
        }
    }   
    close(fd);
}

  • 结构体数组的读写
#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
#define NAME_LEN 32
typedef struct Student
{
    int iId;
    char caName[NAME_LEN];
    char cSex;
    float fScore;

}Student;

int myOpen(const char *pathname)
{
    int fd  = -1;
    if (NULL != pathname)
    {
        fd = open(pathname, O_WRONLY | O_CREAT
                  , S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
        if (-1 == fd)
        {
            printf("open error: %s\n", strerror(errno));
        }
    }
    return fd;
}
int myWrite(int fd, char *pData, int iTotalSize)
{
    //对形参的值进行有效性检查
    if (-1 != fd && NULL != pData && iTotalSize > 0)
    {
        int i = 0;
        //若数据量比较大是,write可能一次性写不玩
        //这种情况下,必须分多次循环的去写

        //剩余要写的字节数
        int iLeft = iTotalSize;
        
        //已写入的字节数
        int iWirted = 0;
        
        //写数据时的返回值,出错返回-1,
        //成功返回实际写入的字节数
        int ret = -1;

        //如果剩余的数据量大于等于PER_WRITE_BYTES
        //则指定该次写入文件的数据量为PER_WRITE_BYTES
        //否则指定为剩余的数据量:iLeft
        if (iLeft >= PER_WRITE_BYTES)
        {
            ret = write(fd, pData, PER_WRITE_BYTES);
        }
        else
        {
            ret = write(fd, pData, iLeft);
        }
        if (-1 == ret)
        {
            printf("write error: %s\n", strerror(errno));
        }
        else
        {
            printf("%d, write %d bytes to file\n", ++i, ret);
            //剩余数据量减去实际写入的数据量
            //得到新的剩余数据量
            iLeft -= ret;

            //已写的数据量加上实际写入的数据量
            //得到新的已写数据量
            iWirted += ret;
            //如果上次写入没有出错并且还有数据没写完
            //则循环接着写
            while (ret && iLeft)
            {
                if (iLeft >= PER_WRITE_BYTES)
                {
                    //指针往后偏移到未写的数据位置
                    //从该位置开始将数据写入文件
                    ret = write(fd, pData+iWirted
                                , PER_WRITE_BYTES);
                }
                else
                {
                    ret = write(fd, pData+iWirted
                                , iLeft);
                }
                if (-1 != ret)
                {
                    iLeft -= ret;
                    iWirted += ret;
                    printf("%d, write %d bytes to file\n"
                           , ++i, ret);
                }
                else
                {
                    printf("write error: %s\n", strerror(errno));
                }
            }
        }
    }
}

int main(void)
{
    int fd = -1;
    fd = myOpen("test.info");
    if (-1 != fd)
    {
        char caBuf[4096789] = {'A'};
        myWrite(fd, caBuf, sizeof(caBuf));
        
        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_READ_BYTES 4096
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 myRead(int fd, char *pData, int iTotalSize)
{
    //对形参的值进行有效性检查
    if (-1 != fd && NULL != pData && iTotalSize > 0)
    {
        int i = 0;
        //若数据量比较大是,read可能一次性读不玩
        //这种情况下,必须分多次循环的去读

        //剩余要读的字节数
        int iLeft = iTotalSize;
        
        //已读取的字节数
        int iReaded = 0;
        
        //读数据时的返回值,出错返回-1,
        //成功返回实际读取的字节数
        int ret = -1;

        //如果剩余的数据量大于等于PER_WRITE_BYTES
        //则指定该次读取文件的数据量为PER_WRITE_BYTES
        //否则指定为剩余的数据量:iLeft
        if (iLeft >= PER_READ_BYTES)
        {
            ret = read(fd, pData, PER_READ_BYTES);
        }
        else
        {
            ret = read(fd, pData, iLeft);
        }
        if (-1 == ret)
        {
            printf("read error: %s\n", strerror(errno));
        }
        else
        {
            printf("%d, read %d bytes to file\n", ++i, ret);
            //剩余数据量减去实际读取的数据量
            //得到新的剩余数据量
            iLeft -= ret;

            //已写的数据量加上实际读取的数据量
            //得到新的已读取数据量
            iReaded += ret;
            //如果上次写入没有出错并且还有数据没读完
            //则循环接着读
            while (ret && iLeft)
            {
                if (iLeft >= PER_READ_BYTES)
                {
                    //指针往后偏移到未写的数据位置
                    //从该位置开始将数据写入文件
                    ret = read(fd, pData+iReaded, PER_READ_BYTES);
                }
                else
                {
                    ret = read(fd, pData+iReaded
                                , iLeft);
                }
                if (-1 != ret)
                {
                    iLeft -= ret;
                    iReaded += ret;
                    
                    printf("%d, read %d bytes to file,left :%d\n", ++i, ret,iReaded);
                }
                else
                {
                    printf("read error: %s\n", strerror(errno));
                }
            }
        }
    }
}

int main(void)
{
    int fd = -1;
    fd = myOpen("test.data");
    if (-1 != fd)
    {
        char caBuf[4096789] = {'A'};
        myRead(fd, caBuf, sizeof(caBuf));
        close(fd);
    }   
    return 0;
}


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,835评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,598评论 1 295
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,569评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,159评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,533评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,710评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,923评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,674评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,421评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,622评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,115评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,428评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,114评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,097评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,875评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,753评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,649评论 2 271

推荐阅读更多精彩内容

  • 文件的操作 1、文件打开 使用open函数打开和创建函数 参数: pathname 待打开文件路径fla...
    Hassan_chao阅读 198评论 0 0
  • 作业 文件的拷贝 readFromSTDIN readFromSTDIN_NoBlock eintr 学生结构体,...
    帅碧阅读 243评论 1 1
  • exec(鸠占鹊巢) 查找文件:fins /usr execl 模拟一个终端,可以同时打开两个文件 模拟一个终端 ...
    帅碧阅读 325评论 1 2
  • 作业:在文件任意位置处插入数据 ACCESS(测试) 测试文件是否存在 测试文件是否有可读可写权限 测试文件是否有...
    帅碧阅读 138评论 1 1
  • 作业 通过无名管道,让两个子进程间完成相互通信工作 命名管道 创建一个命名管道 在命名管道里插入数据 首先在一个终...
    帅碧阅读 144评论 1 1