美文网首页
系统编程-文件操作

系统编程-文件操作

作者: 帅碧 | 来源:发表于2016-12-01 16:22 被阅读0次

系统调用

  • 系统调用主要进行进程控制(系统调用是系统提供用户的一个接口)
  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;
}


相关文章

  • 进程线程-导读

    一:引出 进程线程与文件IO一起,一般也叫做系统编程,系统编程,顾名思义,就是与操作系统有关系的编程,那么系统编程...

  • 系统编程-文件操作

    系统调用 系统调用主要进行进程控制(系统调用是系统提供用户的一个接口) flag:O_RDONLY,O_WRONL...

  • 系统编程-------文件操作

    文件的操作 1、文件打开 使用open函数打开和创建函数 参数: pathname 待打开文件路径fla...

  • 2018-02-11 mac 系统的中常见命令行教程

    mac 系统的中常见命令行教程 目录操作 文件操作 选择操作 安全操作 编程操作 进程操作 时间操作 网络与通信操...

  • (五)Python File(文件)

    前言:文件系统是操作系统的重要组成部分,它规定了计算机对文件和目录进行操作处理的各种标准和机制。以此为基础,编程语...

  • 系统编程-文件操作3

    作业:在文件任意位置处插入数据 ACCESS(测试) 测试文件是否存在 测试文件是否有可读可写权限 测试文件是否有...

  • 系统编程-文件操作2

    作业 文件的拷贝 readFromSTDIN readFromSTDIN_NoBlock eintr 学生结构体,...

  • 系统编程-文件操作4

    exec(鸠占鹊巢) 查找文件:fins /usr execl 模拟一个终端,可以同时打开两个文件 模拟一个终端 ...

  • 系统编程-文件操作5

    作业 通过无名管道,让两个子进程间完成相互通信工作 命名管道 创建一个命名管道 在命名管道里插入数据 首先在一个终...

  • Bash编程014——文件管理

    Bash编程014——文件管理 Unix/Linux系统中将几乎一切都是为文件,所有操作都离不开文件,因而学习管理...

网友评论

      本文标题:系统编程-文件操作

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