文件I/O

學習筆記,小白可以相互學習,大佬看到能告訴咱理解不對的地方就好了。


文件I/O不同於標準I/O是不帶緩衝的,即是每個read和write都調用內核中的相應系統調用。

對於內核而言,所有的打開文件都是有文件描述符引用。文件描述符就是一個非負整數。


函數:

1.open

int open(const char *pathname,int flages)

int open(const char *pathname,int flages,mode_t mode)

int creat(const char *pathname,mode_t mode)

頭文件:sys/types.h sys/stat.h fcntl.h

打開一個文件open()和創建一個文件create()成功返回文件描述符,失敗返回-1,並設置errno。

creat()等價於open(pathname,O_CREAT | O_WRONLY | O_TRNUC,mode)

open()可以打開設備文件,但是不能創建設備文件,設備文件必須用mknod()創建

open的參數:

flash:

1.O_RDNOLY只讀方式打開文件

2.O_WRNOLY只寫方式打開文件

3.O_RDWR讀寫方式打開文件    //前面這三個參數互斥,即不能同時存在

4.O_CREAT如果文件不存在,則建立,並且設置mode

//不常用 O_EXCL如果使用O_CREAT是文件存在,則返回錯誤信息,這個參數可以檢測文件是否存在

//不常用 O_NOCTTY使用本參數時,如果文件爲終端,那麼終端不可以作爲調用open()系統調用的那個進程控制終端

5.O_TRUNC如果文件已經存在,那麼刪除原來文件中的數據(清空)

6.O_APPEND以添加方式打開文件,所以對文件的寫操作都是在文件末尾進行的

mode:

被打開文件的存取權限,爲8進製表示法。例如0777(滿權限),只用在使用O_CREAT參數的時候纔會用到。


2.close

#include<unistd.h>

int close(int files);

可以關閉一個打開的文件,調用成功返回0,出錯返回-1,並設置errno


3.read

#include<unistd.h>

ssize_t read(int fd,void *buf,size_t count);

讀取一個已經打開的文件中的數據,調用成功返回讀取的字節數,如果到達文件末尾返回0,出錯返回-1,並設置errno


4.write

#include<unistd.h>

ssize_t write(int fd,void *buf,size_t count);

向一個已經打開的可寫文件中寫入數據,調用成功返回寫入的字節數,失敗返回-1,並設置errno


5.lseek

#include<unistd.h>

#include<sys/types.h>

off_t lseek(int fd,off_t offset,int whence);

fd文件描述符,offset偏移量(單位byte),whence當前位置基點(SEEK_SET 文件開頭,SEE_CUR當前位置, SEEK_END文件尾)

調用成功:文件當前位置,失敗:-1
注意:lseek只對常規文件有效
#include<stdio.h>
#include <string.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
    if (argc < 3) {
        fprintf(stderr, "Usage: %s <src_file> <dest_file>\n",argv[0]);
return -1;
    }

    int fin;
    int fout;
    fin = open(argv[1],O_RDONLY);
    if(-1 == fin)
    {
     //   printf("file %s can't open",argv[1]);
perror("open file1");
  return -1;
    }
    if((fout = open(argv[2],O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1)
    {
        perror("open file2");
        return -1;
    }

    int ret;
    char buf[100];
    while(1)
    {
ret = read(fin, buf, sizeof(buf));
if (ret < 0) {
perror("read");
break;
}else if (ret == 0) {
printf("read file end!\n");
break;
}
if (ret != write(fout, buf, ret)) {
perror("write");
break;
}
    }

    close(fin);
    close(fout);
}

/*****************上下這兩個代碼都是實現copy功能**************************************/

#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
int main(int argc,char *argv[])
{
    if(argc < 3)
    {
        printf("將filename1中的內容拷貝到filename2中去\n");
        fprintf(stderr,"usage:%s filename1 filename2\n",argv[0]);
        return -1;
    }
    int fd;
    if( 0 > (fd = open(argv[1],O_RDWR)))
    {
        perror("open");
        return -1;
    }

    int fd1;
    if( 0 > (fd1 = open(argv[2],O_CREAT|O_RDWR|O_TRUNC,0664)))
    {
        perror("open");
        return -1;
    }

    char buf[100];
    int  ret;
    int r;
    while(1)
    {
        ret = read(fd,buf,sizeof(buf));
        if(ret < 0)
        {
            perror("read");
        }
        else if( 0 == ret)
        {
            printf("read file end\n");
            break;
        }
        
        r = write(fd1,buf,ret);
        if( ret != r)
        {
           perror("write");
           break;
        }
    }
    close(fd);
    close(fd1);
    return 0;
}

/*************下面是creat***********************************/

#include<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
int main()
{
    int fd;
    if( 0 > (fd = creat("test.txt",644)))
    {
        perror("creat");
        return -1;
    }
    printf("fd = %d\n",fd);
    return 0;
}

/*************lseek**************************/

#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
int main(int argc,char *argv[])
{
    if(argc < 2)
    {
        fprintf(stderr,"usage:%s filename\n",argv[0]);
        return -1;
    }
    int fd;
    if( 0 > (fd = open(argv[1],O_RDONLY)))
    {
        perror("open");
        return -1;
    }

    lseek(fd,100,SEEK_SET);

    char buf[100];
    int  ret;
    while(1)
    {
        ret = read(fd,buf,sizeof(buf));
        if(ret < 0)
        {
            perror("read");
        }
        else if( 0 ==ret)
        {
            printf("read file end\n");
            break;
        }

        if( ret != write(1,buf,ret))
        {
           perror("write");
           break;
        }
    }
    close(fd);
    return 0;
}

/**********************mywrite函數的思想**************************/

#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
int mywrite(int fd);//將內容全部讀入進去
int main(int argc,char *argv[])
{
    if(argc < 2)
    {
        fprintf(stderr,"usage:%s filename\n",argv[0]);
        return -1;
    }
    int fd;
    if( 0 > (fd = open(argv[1],O_RDONLY)))
    {
        perror("open");
        return -1;
    }

    char buf[100];
    int  ret;
    while(1)
    {
        ret = read(fd,buf,sizeof(buf));
        if(ret < 0)
        {
            perror("read");
        }
        else if( 0 ==ret)
        {
            printf("read file end\n");
            break;
        }

        if( ret != write(1,buf,ret))
        //if( ret != mywrite(fd))
        {
           perror("write");
           break;
        }
    }
    close(fd);
    return 0;
}

int mywrite(int fd)//將內容全部讀入進去
{
    int ret;
    int buf[100];
    int count = 0;
    while( count < sizeof(buf))//如果沒有全部讀入則進行循環,直到全部讀入
    {
        ret = write(fd,buf + count,sizeof(buf) - count);
        count = count +  ret;
    }
    return count;
}


/*************求能打開的最多文件數目:1024個*******************************/

#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
int main()
{
    int i = 3,a;
    while(1)
    {
        if( 0 > (a = open("11.c",O_CREAT | O_WRONLY | O_TRUNC,666)))
        {
            perror("open");
            break;
        }
        i++;
        printf("%d\n",i);
    }
    close(a);
    return 0;
}




發佈了31 篇原創文章 · 獲贊 21 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章