網(wǎng)站開發(fā)常去的論壇寧波網(wǎng)站推廣網(wǎng)站優(yōu)化
文章目錄
- 一、獲取文件長度
- 二、追加寫入
- 三、覆蓋寫入
- 四、文件創(chuàng)建函數(shù)creat
一、獲取文件長度
通過lseek函數(shù),除了操作定位文件指針,還可以獲取到文件大小,注意這里是文件大小,單位是字節(jié)。例如在file1文件中事先寫入"你好世界!",那么在gbk編碼的情況下,一個(gè)中文字符占3個(gè)字節(jié),獲取到的文件大小就是3*5=15字節(jié)。
上述代碼如下:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>int main()
{int fd;char *buf = "chenLichen hen shuai!"; fd = open("./file1",O_RDWR);int filesize = lseek(fd, 0, SEEK_END);printf("file's size is:%d\n",filesize);close(fd);return 0;
}
二、追加寫入
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>int main()
{int fd; // 聲明文件描述符變量char *buf = "chenLichen hen shuai!"; // 聲明一個(gè)字符串指針,并賦值一個(gè)字符串常量// 以讀寫和追加方式打開(如果文件不存在則創(chuàng)建)名為 "file1" 的文件fd = open("./file1", O_RDWR | O_APPEND);// 打印文件打開是否成功的信息和文件描述符printf("open success : fd = %d\n", fd);// 將字符串 buf 中的內(nèi)容寫入到打開的文件中int n_write = write(fd, buf, strlen(buf));if (n_write != -1) {printf("write %d byte to file\n", n_write); // 打印成功寫入文件的字節(jié)數(shù)}close(fd); // 關(guān)閉文件描述符對應(yīng)的文件return 0;
}
這段代碼的主要操作包括:
-
文件打開:
- 使用
open
函數(shù)以讀寫和追加的方式打開名為 “file1” 的文件,如果文件不存在則創(chuàng)建。 O_RDWR
標(biāo)志表示以讀寫方式打開文件,O_APPEND
標(biāo)志表示在文件末尾追加數(shù)據(jù)。
- 使用
-
寫入文件:
- 將字符串 “chenLichen hen shuai!” 的內(nèi)容寫入到打開的文件中。
- 使用
write
函數(shù)將數(shù)據(jù)寫入文件,并獲取成功寫入的字節(jié)數(shù)。
-
文件關(guān)閉:
- 使用
close
函數(shù)關(guān)閉文件描述符,釋放相關(guān)資源。
- 使用
這段代碼的目的是打開一個(gè)文件,將指定的字符串內(nèi)容追加到文件末尾,并輸出寫入文件的字節(jié)數(shù)。
三、覆蓋寫入
以下是代碼的注釋和解釋:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>int main()
{int fd; // 聲明文件描述符變量char *buf = "test"; // 聲明一個(gè)字符串指針,并賦值一個(gè)字符串常量// 以讀寫和截?cái)喾绞酱蜷_(如果文件不存在則創(chuàng)建)名為 "file1" 的文件fd = open("./file1", O_RDWR | O_TRUNC);// 打印文件打開是否成功的信息和文件描述符printf("open success : fd = %d\n", fd);// 將字符串 buf 中的內(nèi)容寫入到打開的文件中int n_write = write(fd, buf, strlen(buf));if (n_write != -1) {printf("write %d byte to file\n", n_write); // 打印成功寫入文件的字節(jié)數(shù)}close(fd); // 關(guān)閉文件描述符對應(yīng)的文件return 0;
}
這段代碼的主要操作包括:
-
文件打開:
- 使用
open
函數(shù)以讀寫和截?cái)嗟姆绞酱蜷_名為 “file1” 的文件,如果文件不存在則創(chuàng)建。 O_RDWR
標(biāo)志表示以讀寫方式打開文件,O_TRUNC
標(biāo)志表示清空文件內(nèi)容(截?cái)辔募?#xff09;。
- 使用
-
寫入文件:
- 將字符串 “test” 的內(nèi)容寫入到打開的文件中。
- 使用
write
函數(shù)將數(shù)據(jù)寫入文件,并獲取成功寫入的字節(jié)數(shù)。
-
文件關(guān)閉:
- 使用
close
函數(shù)關(guān)閉文件描述符,釋放相關(guān)資源。
- 使用
這段代碼的目的是打開一個(gè)文件,在以讀寫方式打開文件的同時(shí)將文件內(nèi)容清空,然后將字符串 “test” 寫入文件,并輸出寫入文件的字節(jié)數(shù)。
四、文件創(chuàng)建函數(shù)creat
以下是代碼的注釋和解釋:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>int main()
{int fd; // 聲明文件描述符變量char *buf = "test"; // 聲明一個(gè)字符串指針,并賦值一個(gè)字符串常量// 使用 creat 函數(shù)創(chuàng)建一個(gè)文件 "/home/CLC/file1",并設(shè)置文件權(quán)限為用戶可讀、寫和執(zhí)行fd = creat("/home/CLC/file1", S_IRWXU);return 0;
}
這段代碼的主要操作包括:
-
文件創(chuàng)建:
- 使用
creat
函數(shù)創(chuàng)建一個(gè)文件 “/home/CLC/file1”。 creat
函數(shù)是一個(gè)對open
函數(shù)的封裝,用于創(chuàng)建文件,如果文件已存在,則將其截?cái)酁榭瘴募?/li>S_IRWXU
是文件權(quán)限參數(shù),表示用戶(擁有者)具有讀、寫和執(zhí)行權(quán)限。
- 使用
-
文件描述符:
creat
函數(shù)成功創(chuàng)建文件后,會(huì)返回一個(gè)文件描述符fd
。- 在這段代碼中并未進(jìn)行其他文件操作,所以文件描述符沒有被使用到其他操作中。
這段代碼的目的是使用 creat
函數(shù)創(chuàng)建一個(gè)名為 “/home/CLC/file1” 的文件,并將文件權(quán)限設(shè)置為用戶可讀、寫和執(zhí)行。