Itop4412學(xué)習(xí)筆記(2)

? 今天學(xué)習(xí)的是文件IO的操作,需要記錄的點(diǎn):

1.庫(kù)函數(shù)頭文件

? 在所有Linux系統(tǒng)中,對(duì)文件的操作都只需包含下面四個(gè)頭文件即可:

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

2.打開(kāi)文件函數(shù)open

? open函數(shù)會(huì)返回一個(gè)文件句柄,下面是函數(shù)open的兩種原型:
? int open(const char *path,int oflags);
? int open(const char *path,int oflags,mode_t mode);

? open函數(shù)有2到3個(gè)參數(shù),其返回值為對(duì)應(yīng)文件的句柄:

? 1.第一個(gè)參數(shù)path為絕對(duì)路徑

? 2.第二個(gè)參數(shù)oflags為讀寫(xiě)方式等操作

  • 下面三個(gè)選項(xiàng)是必須選擇其中之一的。
  O_RDONLY 文件只讀
  O_WRONLY 文件只寫(xiě)
  O_RDWR 文件可讀可寫(xiě)
  • 下面是可以任意選擇的。
  O_APPEND 每次寫(xiě)操作都寫(xiě)入文件的末尾
  O_CREAT 如果指定文件不存在,則創(chuàng)建這個(gè)文件
  O_EXCL 如果要?jiǎng)?chuàng)建的文件已存在,則返回-1,并且修改errno 的值
  O_TRUNC 如果文件存在,并且以只寫(xiě)/讀寫(xiě)方式打開(kāi),則清空文件全部?jī)?nèi)容
  O_NOCTTY 如果路徑名指向終端設(shè)備,不要把這個(gè)設(shè)備用作控制終端。
  O_NONBLOCK 如果路徑名指向FIFO/塊文件/字符文件,則把文件的打開(kāi)和后繼I/O設(shè)置為非阻塞模式
  O_NDELAY 和O_NONBLOCK 功能類(lèi)似,調(diào)用O_NDELAY 和使用的O_NONBLOCK 功能是一樣的。

? 3.第三個(gè)參數(shù)mode為設(shè)置創(chuàng)建文件的權(quán)限。

    其參數(shù)設(shè)置可參照l(shuí)inux下的權(quán)限值,例如chmod 777 filename

? 下面是一個(gè)open的測(cè)試?yán)蹋?/p>

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

main()
{
    int     fd;
    char    *leds = "/dev/leds";
    char    *test1 = "/bin/test1";
    char    *test2 = "/bin/test2";
    
    if((fd = open(leds,O_RDWR|O_NOCTTY|O_NDELAY))<0)
    {
        printf("open %s failed!\n",leds);
    }
    
    printf("\n%s fd is %d\n",leds,fd);
    
    if((fd = open(test1,O_RDWR,0777))<0)
    {
        printf("open %s failed!\n",test1);
    }
    
    printf("%s fd is %d\n",test1,fd);
    
    if((fd = open(test2,O_RDWR|O_CREAT,0777))<0)
    {
        printf("open %s failed!\n",test2);
    }
    
    printf("%s fd is %d\n",test2,fd);
}

? 如果/bin/目錄下已經(jīng)存在leds和test1文件,那么三個(gè)fd都會(huì)返回對(duì)應(yīng)的值,否則只有test2能返回句柄值。

3.創(chuàng)建文件函數(shù) creat

? creat函數(shù)用法與open函數(shù)相同且完全可以用open函數(shù)代替,現(xiàn)在已經(jīng)很少使用creat函數(shù),下面是其函數(shù)原型:

int creat(const char *path,mode_t mode);

? creat函數(shù)只有兩個(gè)參數(shù),返回值為對(duì)應(yīng)文件句柄,其參數(shù)定義可參照open函數(shù):

  • 第一個(gè)參數(shù)path為絕對(duì)路徑
  • 第二個(gè)參數(shù)mode為設(shè)置創(chuàng)建文件的權(quán)限

? 其測(cè)試?yán)踢@里不再貼出

4.關(guān)閉文件函數(shù)close

? 任何一個(gè)文件在操作完成之后都需要關(guān)閉,即通過(guò)close函數(shù)來(lái)實(shí)現(xiàn),調(diào)用close 函數(shù)之后,會(huì)取消open 函數(shù)建立的映射關(guān)系,句柄將不再有效,占用的空間將被系統(tǒng)釋放。下面是其函數(shù)原型:

int close(int fd);

? clsoe函數(shù)的參數(shù)很簡(jiǎn)單且只有一個(gè),返回值一般很少使用:

  • 參數(shù)fd為使用open函數(shù)打開(kāi)對(duì)應(yīng)文件所返回的句柄

? 其調(diào)用方法較為簡(jiǎn)單,例程在后續(xù)實(shí)驗(yàn)中會(huì)用到。

5.寫(xiě)入文件函數(shù)write

? 對(duì)已經(jīng)打開(kāi)的文件進(jìn)行寫(xiě)入操作的函數(shù),其函數(shù)原型為:

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

? write函數(shù)有三個(gè)參數(shù)一個(gè)返回值,返回值類(lèi)型為ssize,出錯(cuò)時(shí)返回-1,其他值返回實(shí)際寫(xiě)入字節(jié)數(shù):

  • 第一個(gè)參數(shù)fd,是對(duì)應(yīng)文件open操作時(shí)返回的句柄
  • 第二個(gè)參數(shù)*buf,是需要寫(xiě)入的數(shù)據(jù)
  • 第三個(gè)參數(shù)count,表示將第二個(gè)參數(shù)*buf中最多count個(gè)字節(jié)寫(xiě)入對(duì)應(yīng)文件

? write函數(shù)的例程如下:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>

main()
{
    int     fd;
    char    *testwrite = "/bin/testwrite";
    ssize_t length_w;
    char    buffer_write[] = "Hello Write Function!";
    
    if((fd = open(testwrite,O_RDWR|O_CREAT,777))<0)
    {
            printf("open %s failed \n",testwrite);
    }
    
    length_w = write(fd,buffer_write,strlen(buffer_write));
    if(length_w == -1)
    {
        perror("write");
    }
    else
    {
        printf("Write Function OK!\n And the length of input is %d",length_w);
    }
    close(fd);
}

? 最終在屏幕上輸出

Write Function OK!
And the length of input is 21

6.讀取文件函數(shù)read

? 對(duì)文件進(jìn)行讀取操作,使用較為頻繁,其函數(shù)原型為:

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

? read函數(shù)有三個(gè)參數(shù)一個(gè)返回值,返回值類(lèi)型為ssize,出錯(cuò)時(shí)返回-1,其他值返回實(shí)際讀取字節(jié)數(shù):

  • 第一個(gè)參數(shù)fd,是對(duì)應(yīng)文件open操作時(shí)返回的句柄
  • 第二個(gè)參數(shù)*buf,是讀取數(shù)據(jù)保存的位置
  • 第三個(gè)參數(shù)len,表示每次從對(duì)應(yīng)文件中讀取最多count個(gè)字節(jié)存入第二個(gè)參數(shù)指定的位置。

? open函數(shù)的例程如下:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>

#define MAX_SIZE    1000

main()
{
    int fd;
    ssize_t length_w,ret,length_r = MAX_SIZE;
    char *testwrite = "/bin/testwrite";
    char buffer_write[] = "Hello Write Function!";
    char buffer_read[MAX_SIZE];
    
    if((fd = open(testwrite,O_RDWR|O_CREAT,777))<0)
    {
        printf("open %s failed!\n",testwrite);
    }
    length_w = write(fd,buffer_write,strlen(buffer_write));
    if(length_w == -1)
    {
        perror("write");
    }
    else
    {
        printf("Write Function OK\n And the length of input is %d !\n",length_w);
    }
    close(fd);
    
    if((fd = open(testwrite,O_RDWR|O_CREAT,777))<0)
    {
        printf("open %s failed!\n",testwrite);
    }
    ret = read(fd,buffer_read,length_r);
    if(ret == -1)
    {
        perror("read");
    }
    else
    {
        printf("Files Content is %s \n And the length of read is %d !\n",buffer_read,ret);
    }
    close(fd);
}

? 最終在屏幕上輸出

Write Function OK
And the length of input is 21 !
Files Content is Hello Write Function! 
And the length of read is 21 !

7.結(jié)束語(yǔ)

? 花了兩個(gè)晚上才完成文件IO操作的學(xué)習(xí),都是很基礎(chǔ)的知識(shí),以后用的也比較多,所以雖然比較簡(jiǎn)單但還是跟著教程一步一步走了一遍,希望能加深印象,共勉~

最后編輯于
?著作權(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)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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