系統(tǒng)編程-文件操作

系統(tǒng)調(diào)用

  • 系統(tǒng)調(diào)用主要進(jìn)行進(jìn)程控制(系統(tǒng)調(diào)用是系統(tǒng)提供用戶的一個(gè)接口)
  1. flag:O_RDONLY,O_WRONLY,O_RDWR
  2. 路徑:
  • 絕對(duì)路徑:以/開始的路徑稱之為絕對(duì)路徑/user/inclue
  • 相對(duì)路徑:以./開始的路徑稱之為相對(duì)路徑,./可以省略不寫./..1612-->../1612
  1. 使用flag指定的方式打開指定路徑的文件
  2. 若文件打開成功,返回該文件的一個(gè)新的文件描述符
  3. 文件描述符用于文件操作
  4. 若文件打開失敗,返回-1
  5. pathname:"./stu.info"
  • 一個(gè)文件的創(chuàng)建,以及文件的屬性

//1_read_only.c

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<stdio.h>
#include<errno.h>//錯(cuò)誤號(hào)erron
#include<string.h>//strerror()
#include<unistd.h>
//flag:O_RDONLY,O_WRONLY,O_RDWR
//路徑:
//絕對(duì)路徑:以/開始的路徑稱之為絕對(duì)路徑
//相對(duì)路徑:以./開始的路徑稱之為相對(duì)路徑,./可以省略不寫
//使用flag指定的方式打開指定路徑的文件
//若文件打開成功,返回該文件的一個(gè)新的文件描述符
//文件描述符用于文件操作
//若文件打開失敗,返回-1
//int open(const char *pathname,int flag);
int main()
{
    int fd = -1;
    //以只讀方式打開文件,若文件不存在,不會(huì)自動(dòng)創(chuàng)建文件,打開失敗
    fd=open("stu.info",O_RDONLY);
    if(-1==fd)
    {
        
        printf("open file failed...\n");
        //當(dāng)打開失敗,會(huì)對(duì)errno設(shè)置錯(cuò)誤值
        //該錯(cuò)誤值代表發(fā)生的錯(cuò)誤
        printf("errno=%d\n",errno);
        //將strerror獲得erron代表的錯(cuò)誤信息
        printf("erron:%s\n",strerror(errno));
    }
    else
    {
        printf("open file ok...\n");
        //關(guān)閉打開的文件描述符:關(guā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>//錯(cuò)誤號(hào)erron
#include<string.h>//strerror()
#include<unistd.h>
//flag:O_RDONLY,O_WRONLY,O_RDWR
//路徑:
//絕對(duì)路徑:以/開始的路徑稱之為絕對(duì)路徑
//相對(duì)路徑:以./開始的路徑稱之為相對(duì)路徑,./可以省略不寫
//使用flag指定的方式打開指定路徑的文件
//若文件打開成功,返回該文件的一個(gè)新的文件描述符
//文件描述符用于文件操作
//若文件打開失敗,返回-1
//int open(const char *pathname,int flag);
int main()
{
    int fd = -1;
    //以只讀方式打開文件,若文件不存在,不會(huì)自動(dòng)創(chuàng)建文件,打開失敗
    fd=open("stu.info",O_RDWR);
    if(-1==fd)
    {
        
        printf("open file failed...\n");
        //當(dāng)打開失敗,會(huì)對(duì)errno設(shè)置錯(cuò)誤值
        //該錯(cuò)誤值代表發(fā)生的錯(cuò)誤
        printf("errno=%d\n",errno);
        //將strerror獲得erron代表的錯(cuò)誤信息
        printf("erron:%s\n",strerror(errno));
        
    }
    else
    {
        printf("open file ok...\n");
        //關(guān)閉打開的文件描述符:關(guā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>//錯(cuò)誤號(hào)erron
#include<string.h>//strerror()
#include<unistd.h>
//flag:O_RDONLY,O_WRONLY,O_RDWR
//路徑:
//絕對(duì)路徑:以/開始的路徑稱之為絕對(duì)路徑
//相對(duì)路徑:以./開始的路徑稱之為相對(duì)路徑,./可以省略不寫
//使用flag指定的方式打開指定路徑的文件
//若文件打開成功,返回該文件的一個(gè)新的文件描述符
//文件描述符用于文件操作
//若文件打開失敗,返回-1
//int open(const char *pathname,int flag);
//mode:
//   S_IRWXU:用戶對(duì)文件具有讀,寫,執(zhí)行的權(quán)限
//   S_IRUSR:用戶對(duì)文件具有讀的權(quán)限
//       S_IWUSR:用戶對(duì)文件具有寫的權(quán)限
//       S_IXUSR:用戶對(duì)文件具有執(zhí)行的權(quán)限 

//   S_IRWXG:用戶對(duì)文件具有讀,寫,執(zhí)行的權(quán)限
//   S_IRGRP:用戶對(duì)文件具有讀的權(quán)限
//       S_IWGRP:用戶對(duì)文件具有寫的權(quán)限
//       S_IXGRP:用戶對(duì)文件具有執(zhí)行的權(quán)限 

//   S_IRWXO:用戶對(duì)文件具有讀,寫,執(zhí)行的權(quán)限
//   S_IROTH:用戶對(duì)文件具有讀的權(quán)限
//       S_IWOTH:用戶對(duì)文件具有寫的權(quán)限
//       S_IXOTH:用戶對(duì)文件具有執(zhí)行的權(quán)限 

//當(dāng)需要指定多個(gè)權(quán)限的時(shí)候,
//使用mode指定的文件權(quán)限的方式在指定的路徑下創(chuàng)建文件
//int create (const char *pathname,mode_t mode);
int main()
{
    int fd = -1;
    //以只讀方式打開文件,若文件不存在,不會(huì)自動(dòng)創(chuàng)建文件,打開失敗
    fd=open("stu.info",O_RDONLY);
    if(-1==fd)
    {
        
        printf("open file failed...\n");
        //當(dāng)打開失敗,會(huì)對(duì)errno設(shè)置錯(cuò)誤值
        //該錯(cuò)誤值代表發(fā)生的錯(cuò)誤
        printf("errno=%d\n",errno);
        //將strerror獲得erron代表的錯(cuò)誤信息
        printf("erron:%s\n",strerror(errno));
        if(2==errno)
        {
            //以用戶具有讀寫權(quán)限,用戶組具有讀權(quán)限
                        //其他用戶具有讀權(quán)限的方式創(chuàng)建文件
                        //相當(dāng)于創(chuàng)建一個(gè)文件,并且以寫的方式打開該文件
                        //并且將該文件內(nèi)容截?cái)啵ㄇ蹇瘴募?            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");
        //關(guān)閉打開的文件描述符:關(guā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>//錯(cuò)誤號(hào)erron
#include<string.h>//strerror()
#include<unistd.h>
//flag:O_RDONLY,O_WRONLY,O_RDWR
//路徑:
//絕對(duì)路徑:以/開始的路徑稱之為絕對(duì)路徑
//相對(duì)路徑:以./開始的路徑稱之為相對(duì)路徑,./可以省略不寫
//使用flag指定的方式打開指定路徑的文件
//若文件打開成功,返回該文件的一個(gè)新的文件描述符
//文件描述符用于文件操作
//若文件打開失敗,返回-1
//int open(const char *pathname,int flag);
//mode:
//   S_IRWXU:用戶對(duì)文件具有讀,寫,執(zhí)行的權(quán)限
//   S_IRUSR:用戶對(duì)文件具有讀的權(quán)限
//       S_IWUSR:用戶對(duì)文件具有寫的權(quán)限
//       S_IXUSR:用戶對(duì)文件具有執(zhí)行的權(quán)限 

//   S_IRWXG:用戶對(duì)文件具有讀,寫,執(zhí)行的權(quán)限
//   S_IRGRP:用戶對(duì)文件具有讀的權(quán)限
//       S_IWGRP:用戶對(duì)文件具有寫的權(quán)限
//       S_IXGRP:用戶對(duì)文件具有執(zhí)行的權(quán)限 

//   S_IRWXO:用戶對(duì)文件具有讀,寫,執(zhí)行的權(quán)限
//   S_IROTH:用戶對(duì)文件具有讀的權(quán)限
//       S_IWOTH:用戶對(duì)文件具有寫的權(quán)限
//       S_IXOTH:用戶對(duì)文件具有執(zhí)行的權(quán)限 

//當(dāng)需要指定多個(gè)不同權(quán)限時(shí),使用“|“連接: S_IRWXU | S_IRGRP
//使用mode指定的文件權(quán)限的方式在指定的路徑下創(chuàng)建文件
//返回該文件新的文件描述符
//int creat(const char *pathname, mode_t mode);
int myOpen(const char *pathname)
{
    
    int fd = -1;
    //以只讀方式打開文件,若文件不存在,不會(huì)自動(dòng)創(chuàng)建文件,打開失敗
    fd=open("stu.info",O_RDONLY);
    if(-1==fd)
    {
        
                if(2==errno)
        {
            //以用戶具有讀寫權(quán)限,用戶組具有讀權(quán)限
                        //其他用戶具有讀權(quán)限的方式創(chuàng)建文件
                        //相當(dāng)于創(chuàng)建一個(gè)文件,并且以寫的方式打開該文件
                        //并且將該文件內(nèi)容截?cái)啵ㄇ蹇瘴募?            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>//錯(cuò)誤號(hào)erron
#include<string.h>//strerror()
#include<unistd.h>
//flag:O_RDONLY,O_WRONLY,O_RDWR
//路徑:
//絕對(duì)路徑:以/開始的路徑稱之為絕對(duì)路徑
//相對(duì)路徑:以./開始的路徑稱之為相對(duì)路徑,./可以省略不寫
//使用flag指定的方式打開指定路徑的文件
//若文件打開成功,返回該文件的一個(gè)新的文件描述符
//文件描述符用于文件操作
//若文件打開失敗,返回-1
//int open(const char *pathname,int flag);
int main()
{
    int fd = -1;
    //以只讀方式打開文件,若文件不存在,不會(huì)自動(dòng)創(chuàng)建文件,打開失敗
    //O_CREAT | O_EXCL:
    //  若文件存在,則創(chuàng)建失敗,并且打開文件失敗
    //  若文件不存在,則創(chuàng)建文件,然后按照指定的打開方式打開文件
    //fd=open("stu.info",O_RDONLY | O_CREAT | O_EXCL,S_IRWXU | S_IWGRP | S_IROTH);


    //O_CREAT:
    //若文件不存在,則按照指定的權(quán)限創(chuàng)建文件,并且打開文件。然后按照指定的打開方式打開文件
    //若文件存在,則按照指定的打開方式打開文件
    fd=open("stu.info",O_RDONLY | O_CREAT,S_IRWXU | S_IWGRP | S_IROTH);
    if(-1==fd)
    {
        
        printf("open file failed...\n");
        //當(dāng)打開失敗,會(huì)對(duì)errno設(shè)置錯(cuò)誤值
        //該錯(cuò)誤值代表發(fā)生的錯(cuò)誤
        printf("errno=%d\n",errno);
        //將strerror獲得erron代表的錯(cuò)誤信息
        printf("erron:%s\n",strerror(errno));
    }
    else
    {
        printf("open file ok...\n");
        //關(guān)閉打開的文件描述符:關(guān)閉文件
        close(fd);
    }
    return 0;
}


//write.c

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<stdio.h>
#include<errno.h>//錯(cuò)誤號(hào)erron
#include<string.h>//strerror()
#include<unistd.h>
//flag:O_RDONLY,O_WRONLY,O_RDWR
//路徑:
//絕對(duì)路徑:以/開始的路徑稱之為絕對(duì)路徑
//相對(duì)路徑:以./開始的路徑稱之為相對(duì)路徑,./可以省略不寫
//使用flag指定的方式打開指定路徑的文件
//若文件打開成功,返回該文件的一個(gè)新的文件描述符
//文件描述符用于文件操作
//若文件打開失敗,返回-1
//int open(const char *pathname,int flag);
int main()
{
    int fd = -1;
    //以只讀方式打開文件,若文件不存在,不會(huì)自動(dòng)創(chuàng)建文件,打開失敗
    //O_CREAT | O_EXCL:
    //  若文件存在,則創(chuàng)建失敗,并且打開文件失敗
    //  若文件不存在,則創(chuàng)建文件,然后按照指定的打開方式打開文件
    //fd=open("stu.info",O_RDONLY | O_CREAT | O_EXCL,S_IRWXU | S_IWGRP | S_IROTH);


    //O_CREAT:
    //若文件不存在,則按照指定的權(quán)限創(chuàng)建文件,并且打開文件。然后按照指定的打開方式打開文件
    //若文件存在,則按照指定的打開方式打開文件
    fd=open("stu.info",O_WRONLY | O_CREAT,S_IRWXU | S_IWGRP | S_IROTH);
    if(-1==fd)
    {
        
        printf("open file failed...\n");
        //當(dāng)打開失敗,會(huì)對(duì)errno設(shè)置錯(cuò)誤值
        //該錯(cuò)誤值代表發(fā)生的錯(cuò)誤
        printf("errno=%d\n",errno);
        //將strerror獲得erron代表的錯(cuò)誤信息
        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);
        }
        //關(guān)閉打開的文件描述符:關(guān)閉文件
        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)
{
    //對(duì)形參的值進(jìn)行有效性檢查
    if (-1 != fd && NULL != pData && iTotalSize > 0)
    {
        int i = 0;
        //若數(shù)據(jù)量比較大是,write可能一次性寫不玩
        //這種情況下,必須分多次循環(huán)的去寫

        //剩余要寫的字節(jié)數(shù)
        int iLeft = iTotalSize;
        
        //已寫入的字節(jié)數(shù)
        int iWirted = 0;
        
        //寫數(shù)據(jù)時(shí)的返回值,出錯(cuò)返回-1,
        //成功返回實(shí)際寫入的字節(jié)數(shù)
        int ret = -1;

        //如果剩余的數(shù)據(jù)量大于等于PER_WRITE_BYTES
        //則指定該次寫入文件的數(shù)據(jù)量為PER_WRITE_BYTES
        //否則指定為剩余的數(shù)據(jù)量: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);
            //剩余數(shù)據(jù)量減去實(shí)際寫入的數(shù)據(jù)量
            //得到新的剩余數(shù)據(jù)量
            iLeft -= ret;

            //已寫的數(shù)據(jù)量加上實(shí)際寫入的數(shù)據(jù)量
            //得到新的已寫數(shù)據(jù)量
            iWirted += ret;
            //如果上次寫入沒有出錯(cuò)并且還有數(shù)據(jù)沒寫完
            //則循環(huán)接著寫
            while (ret && iLeft)
            {
                if (iLeft >= PER_WRITE_BYTES)
                {
                    //指針往后偏移到未寫的數(shù)據(jù)位置
                    //從該位置開始將數(shù)據(jù)寫入文件
                    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)
{
    //對(duì)形參的值進(jìn)行有效性檢查
    if (-1 != fd && NULL != pData && iTotalSize > 0)
    {
        int i = 0;
        //若要讀的數(shù)據(jù)量比較大時(shí),read可能一次性讀不完
        //這種情況下,必須分多次循環(huán)的去讀

        //剩余要讀的字節(jié)數(shù)
        int iLeft = iTotalSize;
        
        //已讀取的字節(jié)數(shù)
        int iReaded = 0;
        
        //讀數(shù)據(jù)時(shí)的返回值,出錯(cuò)返回-1,
        //成功返回實(shí)際讀取的字節(jié)數(shù)
        int ret = -1;

        //如果剩余的數(shù)據(jù)量大于等于PER_IO_BYTES
        //則指定該次讀取文件的數(shù)據(jù)量為PER_IO_BYTES
        //否則指定為剩余的數(shù)據(jù)量: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);
            //剩余數(shù)據(jù)量減去實(shí)際讀取的數(shù)據(jù)量
            //得到新的剩余數(shù)據(jù)量
            iLeft -= ret;

            //已讀取的數(shù)據(jù)量加上實(shí)際讀取的數(shù)據(jù)量
            //得到新的已讀取數(shù)據(jù)量
            iReaded += ret;
            //如果上次讀取沒有出錯(cuò)并且還有數(shù)據(jù)沒讀取完
            //則循環(huán)接著讀
            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:要進(jìn)行讀操作的文件的文件描述符
//buf:存放讀的數(shù)據(jù)的空間首地址
//count:指定本次要從文件中讀取多少個(gè)字節(jié)
//read出錯(cuò)返回-1,成功返回實(shí)際讀取的字節(jié)數(shù)
//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;
}

  • 學(xué)生信息的讀寫

#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);
}

  • 結(jié)構(gòu)體數(shù)組的讀寫
#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)
{
    //對(duì)形參的值進(jìn)行有效性檢查
    if (-1 != fd && NULL != pData && iTotalSize > 0)
    {
        int i = 0;
        //若數(shù)據(jù)量比較大是,write可能一次性寫不玩
        //這種情況下,必須分多次循環(huán)的去寫

        //剩余要寫的字節(jié)數(shù)
        int iLeft = iTotalSize;
        
        //已寫入的字節(jié)數(shù)
        int iWirted = 0;
        
        //寫數(shù)據(jù)時(shí)的返回值,出錯(cuò)返回-1,
        //成功返回實(shí)際寫入的字節(jié)數(shù)
        int ret = -1;

        //如果剩余的數(shù)據(jù)量大于等于PER_WRITE_BYTES
        //則指定該次寫入文件的數(shù)據(jù)量為PER_WRITE_BYTES
        //否則指定為剩余的數(shù)據(jù)量: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);
            //剩余數(shù)據(jù)量減去實(shí)際寫入的數(shù)據(jù)量
            //得到新的剩余數(shù)據(jù)量
            iLeft -= ret;

            //已寫的數(shù)據(jù)量加上實(shí)際寫入的數(shù)據(jù)量
            //得到新的已寫數(shù)據(jù)量
            iWirted += ret;
            //如果上次寫入沒有出錯(cuò)并且還有數(shù)據(jù)沒寫完
            //則循環(huán)接著寫
            while (ret && iLeft)
            {
                if (iLeft >= PER_WRITE_BYTES)
                {
                    //指針往后偏移到未寫的數(shù)據(jù)位置
                    //從該位置開始將數(shù)據(jù)寫入文件
                    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)
{
    //對(duì)形參的值進(jìn)行有效性檢查
    if (-1 != fd && NULL != pData && iTotalSize > 0)
    {
        int i = 0;
        //若數(shù)據(jù)量比較大是,read可能一次性讀不玩
        //這種情況下,必須分多次循環(huán)的去讀

        //剩余要讀的字節(jié)數(shù)
        int iLeft = iTotalSize;
        
        //已讀取的字節(jié)數(shù)
        int iReaded = 0;
        
        //讀數(shù)據(jù)時(shí)的返回值,出錯(cuò)返回-1,
        //成功返回實(shí)際讀取的字節(jié)數(shù)
        int ret = -1;

        //如果剩余的數(shù)據(jù)量大于等于PER_WRITE_BYTES
        //則指定該次讀取文件的數(shù)據(jù)量為PER_WRITE_BYTES
        //否則指定為剩余的數(shù)據(jù)量: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);
            //剩余數(shù)據(jù)量減去實(shí)際讀取的數(shù)據(jù)量
            //得到新的剩余數(shù)據(jù)量
            iLeft -= ret;

            //已寫的數(shù)據(jù)量加上實(shí)際讀取的數(shù)據(jù)量
            //得到新的已讀取數(shù)據(jù)量
            iReaded += ret;
            //如果上次寫入沒有出錯(cuò)并且還有數(shù)據(jù)沒讀完
            //則循環(huán)接著讀
            while (ret && iLeft)
            {
                if (iLeft >= PER_READ_BYTES)
                {
                    //指針往后偏移到未寫的數(shù)據(jù)位置
                    //從該位置開始將數(shù)據(jù)寫入文件
                    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;
}


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 文件的操作 1、文件打開 使用open函數(shù)打開和創(chuàng)建函數(shù) 參數(shù): pathname 待打開文件路徑fla...
    Hassan_chao閱讀 297評(píng)論 0 0
  • 作業(yè) 文件的拷貝 readFromSTDIN readFromSTDIN_NoBlock eintr 學(xué)生結(jié)構(gòu)體,...
    帥碧閱讀 301評(píng)論 1 1
  • exec(鳩占鵲巢) 查找文件:fins /usr execl 模擬一個(gè)終端,可以同時(shí)打開兩個(gè)文件 模擬一個(gè)終端 ...
    帥碧閱讀 372評(píng)論 1 2
  • 作業(yè):在文件任意位置處插入數(shù)據(jù) ACCESS(測(cè)試) 測(cè)試文件是否存在 測(cè)試文件是否有可讀可寫權(quán)限 測(cè)試文件是否有...
    帥碧閱讀 194評(píng)論 1 1
  • 作業(yè) 通過無名管道,讓兩個(gè)子進(jìn)程間完成相互通信工作 命名管道 創(chuàng)建一個(gè)命名管道 在命名管道里插入數(shù)據(jù) 首先在一個(gè)終...
    帥碧閱讀 185評(píng)論 1 1

友情鏈接更多精彩內(nèi)容