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

  • 作業(yè)
  • 文件的拷貝
#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_IO_BYTES 4096
int myOpen(const char *pathname,mode_t mode)
{
    int fd  = -1;
    if (NULL != pathname)
    {
        fd = open(pathname, mode | O_CREAT
                  , S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
        if (-1 == fd)
        {
            printf("open error: %s\n", strerror(errno));
        }
    }
    return fd;
}
int main(int argc,char *argv[])
{
    int fdForRead=-1;
    fdForRead=myOpen(argv[1],O_RDONLY);
    if(-1==fdForRead)
    {
        return -1;
    }
    int fdForWrite=-1;
    fdForWrite=myOpen(argv[2],O_WRONLY);
    if(-1==fdForWrite)
    {
        close(fdForRead);
        return -1;  
    }
    unsigned char caBuf[PER_IO_BYTES]={'\0'};
    int ret=0;
    while(1)
    {
        memset(caBuf,'\0',PER_IO_BYTES);
        ret =read(fdForRead,caBuf,PER_IO_BYTES);
        if(-1==ret)
        {
            printf("read error:%s\n",strerror(errno));
            return -1;
        }
        else if(0==ret)
        {
            printf("copy file success\n");
            break;
        }
        else
        {
            printf("read %d bytes from %s\n",ret,argv[1]);
            ret=write(fdForWrite,caBuf,ret);
            if(-1==ret)
            {
                printf("write error:%s\n",strerror(errno));
                return -1;
            }
            printf("write %d bytes from %s\n",ret,argv[2]);
        }
                
        
    }
    return 0;
}

  • readFromSTDIN

#include <stdio.h>
#include <errno.h>  //errno  
#include <string.h>  //strerror()
#include <unistd.h>  //read()  write()
/*open*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#if 0
//頭文件: /usr/include/unistd.h
/* Standard file descriptors.  */
#define STDIN_FILENO    0   /* Standard input.  */
#define STDOUT_FILENO   1   /* Standard output.  */
#define STDERR_FILENO   2   /* Standard error output.  */
#endif
//默認打開的三個文件:
//標準輸入, 標準輸出卫键,標準錯誤輸出
//標準輸入:0 -- STDIN_FILENO
//標準輸出:1 -- STDOUT_FILENO
//標準錯誤輸出:2  -- STDERR_FILENO


int main(int argc, char *argv[])
{
    char caBuf[32] = {'\0'};
    //read:默認阻塞讀終端,
    //     即若標準輸入沒有數(shù)據(jù)可讀,則阻塞直到有數(shù)據(jù)到來
    //     阻塞即為等待數(shù)據(jù)的輸入
    //     可以結(jié)合scanf()函數(shù)來理解
    int ret = read(STDIN_FILENO, caBuf, sizeof(caBuf));
    if (-1 == ret)
    {
        printf("read error:%s\n", strerror(errno));
    }   
    else
    {
        if (caBuf[31] != '\0')
        {
            caBuf[31] = '\0';
        }
        printf("%s\n", caBuf);
    }

    return 0;
}

  • readFromSTDIN_NoBlock
#include <stdio.h>
#include <errno.h>  //errno  
#include <string.h>  //strerror()
#include <unistd.h>  //read()  write()
/*open*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
//頭文件:/usr/include/unistd.h
//默認打開的三個文件:
//標準輸入,標準輸出,標準錯誤輸入
//標準輸入:0---STDIN_FILENO
//標準輸出:1---STDOUT_FILENO
//標準錯誤輸入:2---STDERR_FILENO
int main(int argc, char *argv[])
{
    int fd=-1;
    fd=open("/dev/tty",O_RDWR | O_NONBLOCK);
    printf("fd=%d\n",fd);

    char caBuf[32]={'\0'};
    int ret=-1;
    //read:默認阻塞讀終端
    //即若標準輸入沒有數(shù)據(jù)刻度,則阻塞直到有數(shù)據(jù)到來
    //阻塞即為等待數(shù)據(jù)的輸入
    //可以結(jié)合scanf函數(shù)來理解
    while(1)
    {
        memset(caBuf,'\0',sizeof(caBuf));
        ret=read(fd,caBuf,sizeof(caBuf));
        if(-1==ret)
        {
            //EAGAIN:當設(shè)置非阻塞時,若沒有數(shù)據(jù)可讀,則出錯
            //設(shè)置errno值為EAGAIN
            if(EAGAIN!=errno)
            {
            //  printf("errno=%d,%d\n",errno,EAGAIN);
                printf("read error:%s\n",strerror(errno));
                break;
            }
            printf("no data for reading\n");
            sleep(1);
            continue;
        }
        else
        {
            if(caBuf[31]!='\0')
            {
                caBuf[31]='\0';
            }
            printf("%s\n",caBuf);
        }
    }
    return 0;
}


eintr


#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>


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 iLeft = iTotalSize;
        int iReaded = 0;
        int ret = -1;
        while (ret=read(fd,pData+iReaded,iLeft) && iLeft)
        {
            if (-1==ret)
            {
                //EINTR:當將要進行io操作時,被信號打斷
                //則io操作失敗,則將errno值設(shè)置為EINTR
                //這種情況下可以重新進行io操作
                if(EINTR==errno||EAGAIN ==errno)
                {
                    continue;
                }
                printf("read errno:%s\n",strerror(errno));
                break;
            }
            iLeft -= ret;
            iReaded += ret;
        }
    }
}
int main(void)
{
    int fd = -1;
    fd = myOpen("test.data");
    if (-1 != fd)
    {
        char caBuf[1000100] = {'\0'};
        myRead(fd, caBuf, sizeof(caBuf));
        close(fd);
    }   

    return 0;
}


  • 學生結(jié)構(gòu)體,寫入6個學生信息,打印1,3,6位同學
  • write.c

#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 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 | O_TRUNC
                  , 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[6] = {
                           {1001, "zhangsan", 'f', 99},
                           {1002, "lisi", 'm', 79},
                           {1003, "wangwu", 'f', 89},
                           {1004,"damazi",'f',69},
                           {1005,"xiaofang",'m',98},
                           {1006,"xiaoming",'f',88}
                         };
        int ret = -1;
        int i = 0;
        for (; i < 6; i++)
        {
            ret = write(fd, stu+i, sizeof(Student));
            if (-1 == ret)
            {
                printf("write error: %s\n", strerror(errno));
            }
            else
            {
                printf("write %d bytes to file\n", ret);
            }
        }
    }   

    return 0;
}


  • read1.c

#include <stdio.h>
#include <unistd.h>  //write()  read()
#include <errno.h>   //errno
#include <string.h>  //strerror()
/*open()*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#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_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)
    {
        int ret = -1;
        int i=0;
        Student stu;
            //將Student對象的空間都設(shè)置為'\0'
            memset(&stu, '\0', sizeof(Student));
            ret = read(fd, &stu, sizeof(Student));
            if(-1!=ret)
            {
                
                printf("id:%d, name:%s, sex:%c, score:%.1f\n"
                   , stu.iId, stu.caName
                   , stu.cSex, stu.fScore);
            }
            off_t offset=lseek(fd,2*sizeof(Student),SEEK_SET);
            if (-1!=offset)
            {   
                memset(&stu, '\0', sizeof(Student));        
                ret = read(fd, &stu, sizeof(Student));
                if(-1!=ret)
                {
                    printf("id:%d, name:%s, sex:%c, score:%.1f\n"
                       , stu.iId, stu.caName
                       , stu.cSex, stu.fScore);
                }
            }
            offset=lseek(fd,5*sizeof(Student),SEEK_SET);
            if (-1!=offset)
            {   
                memset(&stu, '\0', sizeof(Student));        
                ret = read(fd, &stu, sizeof(Student));
                if(-1!=ret)
                {
                    printf("id:%d, name:%s, sex:%c, score:%.1f\n"
                       , stu.iId, stu.caName
                       , stu.cSex, stu.fScore);
                }
            }
        close(fd);
    }   

    return 0;
}

  • read2.c(第2種方法)
#include <stdio.h>
#include <unistd.h>  //write()  read()
#include <errno.h>   //errno
#include <string.h>  //strerror()
/*open()*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#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_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)
    {
        int ret = -1;
        int i=0;
        Student stu;
        while(1)
        {
            //將Student對象的空間都設(shè)置為'\0'
            memset(&stu, '\0', sizeof(Student));
            ret = read(fd, &stu, sizeof(Student));
            if(-1!=ret)
            {
                
                printf("id:%d, name:%s, sex:%c, score:%.1f\n"
                   , stu.iId, stu.caName
                   , stu.cSex, stu.fScore);
            }
            off_t offset=lseek(fd,sizeof(stu),SEEK_CUR);
            if (-1!=offset)
            {   
                memset(&stu, '\0', sizeof(Student));        
                ret = read(fd, &stu, sizeof(Student));
                if(-1!=ret)
                {
                    printf("id:%d, name:%s, sex:%c, score:%.1f\n"
                       , stu.iId, stu.caName
                       , stu.cSex, stu.fScore);
                }
            }
            offset=lseek(fd,2*sizeof(stu),SEEK_CUR);
            if (-1!=offset)
            {   
                memset(&stu, '\0', sizeof(Student));        
                ret = read(fd, &stu, sizeof(Student));
                if(-1!=ret)
                {
                    printf("id:%d, name:%s, sex:%c, score:%.1f\n"
                       , stu.iId, stu.caName
                       , stu.cSex, stu.fScore);
                }
            }
            break;
        }
        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>
int main(int argc,char *argv[])
{
    int fd=-1;
    fd=open(argv[1],O_RDWR | O_CREAT,S_IRUSR | S_IWUSR | S_IRGRP);
    if(-1 == fd)
    {
        printf("open error:%s\n",strerror(errno));
        return -1;
    }
    //偏移參照位置
    //文件首部,當前位置,文件尾
    //返回值為距離文件首的偏移量
    //第一個參數(shù):文件描述符
    //第二個參數(shù):相對參照位置的偏移量
    //      當偏移為正數(shù)時,往參照位置后偏移
    //      當偏移為負數(shù)時,往參照位置前偏移
    //第三個參數(shù):參照位置SEEK_SET
    //第三個參數(shù):參照位置
    off_t offset = lseek(fd,0,SEEK_END);//返回值為距離文件頭的偏移量
    if(-1==offset)
    {
        printf("lseek error:%s\n",strerror(errno));
    }
    else
    {
        printf("file size:%ld\n",offset);
    }
    return 0;
}
//./a.out test.data
//1234567890abcdefghijklmn
//file size=25

  • 將文件的某個位置上的插入數(shù)據(jù)(覆蓋)
#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>
int main(int argc,char *argv[])
{
    int fd=-1;
    //當打開一個文件時,讀寫位置默認在文件首部
    fd=open(argv[1],O_RDWR | O_CREAT,S_IRUSR | S_IWUSR | S_IRGRP);
    if(-1 == fd)
    {
        printf("open error:%s\n",strerror(errno));
        return -1;
    }
    //偏移參照位置
    //文件首部,當前位置,文件尾
    //返回值為距離文件首的偏移量
    //第一個參數(shù):文件描述符
    //第二個參數(shù):相對參照位置的偏移量
    //      當偏移為正數(shù)時,往參照位置后偏移
    //      當偏移為負數(shù)時,往參照位置前偏移
    //第三個參數(shù):參照位置SEEK_SET
    //第三個參數(shù):參照位置
    off_t offset = lseek(fd,0,SEEK_END);//返回值為距離文件頭的偏移量
    if(-1==offset)
    {
        printf("lseek error:%s\n",strerror(errno));
    }
    else
    {
        printf("file size:%ld\n",offset);
    }
    //將讀寫位置偏移到
    offset=lseek(fd,10,SEEK_SET);
    printf("offset=%ld\n",offset);
    char *iData="888";
    write(fd,iData,strlen(iData));
    close(fd);
    return 0;
}
//./a.out test.data
//原來的為:1234567890abcdefghijklmn
//1234567890888defghijklmn


#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>
int main(int argc,char *argv[])
{
    int fd=-1;
    //當打開一個文件時,讀寫位置默認在文件首部
    fd=open(argv[1],O_RDWR | O_CREAT,S_IRUSR | S_IWUSR | S_IRGRP);
    if(-1 == fd)
    {
        printf("open error:%s\n",strerror(errno));
        return -1;
    }
    //偏移參照位置
    //文件首部,當前位置,文件尾
    //返回值為距離文件首的偏移量
    //第一個參數(shù):文件描述符
    //第二個參數(shù):相對參照位置的偏移量
    //      當偏移為正數(shù)時,往參照位置后偏移
    //      當偏移為負數(shù)時,往參照位置前偏移
    //第三個參數(shù):參照位置SEEK_SET
    //第三個參數(shù):參照位置
    off_t offset = lseek(fd,0,SEEK_END);//返回值為距離文件頭的偏移量
    if(-1==offset)
    {
        printf("lseek error:%s\n",strerror(errno));
    }
    else
    {
        printf("file size:%ld\n",offset);
    }
    //將讀寫位置偏移到第10位,輸入888,將abc替換成了888
    offset=lseek(fd,10,SEEK_SET);
    printf("offset=%ld\n",offset);
    char *iData="888";
    write(fd,iData,strlen(iData));
    //往后5個位置插入666,將678替換為666
    lseek(fd,5,SEEK_SET);
    write(fd,"666",3);
    close(fd);
    return 0;
}
// ./a.out test.data
//原來的為:1234567890abcdefghijklmn
//1234566690888defghijklmn

  • 在一個文件插入數(shù)據(jù),已追加的方式插入
#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_WRONLY | O_CREAT | O_APPEND
                  , 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("test.data");
    char caBuf[10]="helloworld";
    int ret=write(fd,caBuf,sizeof(caBuf));
    if(ret==-1)
    {
        printf("write error:%s\n",strerror(errno)); 
    }
    else
    {
        printf("write %d bytes to file\n",ret);
    }
    close(fd);
}

//helloworldhelloworldhelloworld

pwrite

#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>
int main(int argc,char *argv[])
{
    int fd=-1;
    //當打開一個文件時,讀寫位置默認在文件首部
    fd=open(argv[1],O_RDWR | O_CREAT,S_IRUSR | S_IWUSR | S_IRGRP);
    if(-1 == fd)
    {
        printf("open error:%s\n",strerror(errno));
        return -1;
    }
    //偏移參照位置
    //文件首部,當前位置,文件尾
    //返回值為距離文件首的偏移量
    //第一個參數(shù):文件描述符
    //第二個參數(shù):相對參照位置的偏移量
    //      當偏移為正數(shù)時,往參照位置后偏移
    //      當偏移為負數(shù)時,往參照位置前偏移
    //第三個參數(shù):參照位置SEEK_SET
    //第三個參數(shù):參照位置
    off_t offset = lseek(fd,0,SEEK_END);//返回值為距離文件頭的偏移量
    if(-1==offset)
    {
        printf("lseek error:%s\n",strerror(errno));
    }
    else
    {
        printf("file size:%ld\n",offset);
    }
    //檔一個程序存在多個運行分支的時候,并且每個運行分支都有可能操作文件時存在以下問題:
    //當A分支使用lseek設(shè)置讀寫位置完成時,
    //A分支的運行暫停,B分支開始運行,并且B分支對文件進行了讀寫操作,從而改變了A分支設(shè)置的讀寫位置
    //當B分支運行暫停.A分支重新接著往后運行時,若A分支要對文件進行讀寫,那么讀寫的位置并不是之前設(shè)置的位置,而是B分支運行后的讀寫位置,從而得不到預期結(jié)果
    
    //原子操作:操作部分们童,要么都執(zhí)行嘀倒,要么都不執(zhí)行
    //問題解決方案:將A分支的的lseek和之后的讀寫操作合成原子操作
    // 1.將操作部分使用鎖,鎖住桨嫁,合成原子操作
    // 2.調(diào)用pread()或者pwrite()函數(shù)來完成操作
    /*lseek(fd,-5,SEEK_END);
    write(fd,"666",3);*/
    char *pData="Hello World";
    ssize_t size=pwrite(fd,pData,strlen(pData),5);
    if(-1==size)
    {
        printf("pwrite error:%s\n",strerror(errno));
    }
    close(fd);
    return 0;
}
//原來為:1234567890
//現(xiàn)在為:12345Hello World

fsync

#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>
/*sync()
syncfs()
fsync()

*/
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 main()
{
    int fd = -1;
    fd = myOpen("test.data");
    if (-1 != fd)
    {
        char *pData="Hello World";
        write(fd, pData, strlen(pData));
        //將緩沖區(qū)的
        int ret=fsync(fd);
        if(-1==ret)
        {
            printf("fsync error:%s\n",strerror(errno));
        }
        close(fd);
    }   
    return 0;
}
//Hello World

pread

#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>
int main(int argc,char *argv[])
{
    int fd=-1;
    //當打開一個文件時,讀寫位置默認在文件首部
    fd=open(argv[1],O_RDWR | O_CREAT,S_IRUSR | S_IWUSR | S_IRGRP);
    if(-1 == fd)
    {
        printf("open error:%s\n",strerror(errno));
        return -1;
    }
    //偏移參照位置
    //文件首部,當前位置,文件尾
    //返回值為距離文件首的偏移量
    //第一個參數(shù):文件描述符
    //第二個參數(shù):相對參照位置的偏移量
    //      當偏移為正數(shù)時,往參照位置后偏移
    //      當偏移為負數(shù)時,往參照位置前偏移
    //第三個參數(shù):參照位置SEEK_SET
    //第三個參數(shù):參照位置
    off_t offset = lseek(fd,0,SEEK_END);//返回值為距離文件頭的偏移量
    if(-1==offset)
    {
        printf("lseek error:%s\n",strerror(errno));
    }
    else
    {
        printf("file size:%ld\n",offset);
    }
    //檔一個程序存在多個運行分支的時候,并且每個運行分支都有可能操作文件時存在以下問題:
    //當A分支使用lseek設(shè)置讀寫位置完成時,
    //A分支的運行暫停,B分支開始運行,并且B分支對文件進行了讀寫操作,從而改變了A分支設(shè)置的讀寫位置
    //當B分支運行暫停.A分支重新接著往后運行時,若A分支要對文件進行讀寫,那么讀寫的位置并不是之前設(shè)置的位置,而是B分支運行后的讀寫位置,從而得不到預期結(jié)果
    
    //原子操作:操作部分贰军,要么都執(zhí)行玻蝌,要么都不執(zhí)行
    //問題解決方案:將A分支的的lseek和之后的讀寫操作合成原子操作
    // 方法1.將操作部分使用鎖,鎖住词疼,合成原子操作
    // 方法2.調(diào)用pread()或者pwrite()函數(shù)來完成操作

    //pread()/pwrite():偏移到距離文件首部n字節(jié)的地方進行讀寫操作

    char *pData="Hello World";
    //第四個參數(shù):從文件首部開始的偏移量
    //第三個參數(shù):要寫入的字節(jié)數(shù)
    ssize_t size=pwrite(fd,pData,strlen(pData),5);
    if(-1==size)
    {
        printf("pwrite error:%s\n",strerror(errno));
    }
    char caBuf[32]={'\0'};
    size=pread(fd,caBuf,3,3);
    if(-1!=size)
    {
        printf("%s\n",caBuf);
    }
    close(fd);
    return 0;
}
//12345Hello World

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末俯树,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子贰盗,更是在濱河造成了極大的恐慌许饿,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,820評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件舵盈,死亡現(xiàn)場離奇詭異陋率,居然都是意外死亡,警方通過查閱死者的電腦和手機秽晚,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,648評論 3 399
  • 文/潘曉璐 我一進店門瓦糟,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人赴蝇,你說我怎么就攤上這事菩浙。” “怎么了扯再?”我有些...
    開封第一講書人閱讀 168,324評論 0 360
  • 文/不壞的土叔 我叫張陵芍耘,是天一觀的道長。 經(jīng)常有香客問我熄阻,道長,這世上最難降的妖魔是什么倔约? 我笑而不...
    開封第一講書人閱讀 59,714評論 1 297
  • 正文 為了忘掉前任秃殉,我火速辦了婚禮,結(jié)果婚禮上浸剩,老公的妹妹穿的比我還像新娘钾军。我一直安慰自己,他們只是感情好绢要,可當我...
    茶點故事閱讀 68,724評論 6 397
  • 文/花漫 我一把揭開白布吏恭。 她就那樣靜靜地躺著,像睡著了一般重罪。 火紅的嫁衣襯著肌膚如雪樱哼。 梳的紋絲不亂的頭發(fā)上哀九,一...
    開封第一講書人閱讀 52,328評論 1 310
  • 那天,我揣著相機與錄音搅幅,去河邊找鬼阅束。 笑死,一個胖子當著我的面吹牛茄唐,可吹牛的內(nèi)容都是我干的息裸。 我是一名探鬼主播,決...
    沈念sama閱讀 40,897評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼沪编,長吁一口氣:“原來是場噩夢啊……” “哼呼盆!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起蚁廓,我...
    開封第一講書人閱讀 39,804評論 0 276
  • 序言:老撾萬榮一對情侶失蹤宿亡,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后纳令,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體挽荠,經(jīng)...
    沈念sama閱讀 46,345評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,431評論 3 340
  • 正文 我和宋清朗相戀三年平绩,在試婚紗的時候發(fā)現(xiàn)自己被綠了圈匆。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,561評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡捏雌,死狀恐怖跃赚,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情性湿,我是刑警寧澤纬傲,帶...
    沈念sama閱讀 36,238評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站肤频,受9級特大地震影響叹括,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜宵荒,卻給世界環(huán)境...
    茶點故事閱讀 41,928評論 3 334
  • 文/蒙蒙 一汁雷、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧报咳,春花似錦侠讯、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,417評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至岩臣,卻和暖如春溜嗜,著一層夾襖步出監(jiān)牢的瞬間宵膨,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,528評論 1 272
  • 我被黑心中介騙來泰國打工粱胜, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留柄驻,地道東北人。 一個月前我還...
    沈念sama閱讀 48,983評論 3 376
  • 正文 我出身青樓焙压,卻偏偏與公主長得像鸿脓,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子涯曲,可洞房花燭夜當晚...
    茶點故事閱讀 45,573評論 2 359

推薦閱讀更多精彩內(nèi)容