- open函數
查看函數原型 man 2 open
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
相關參數用法介紹;
a. pathname 文件名
b. flags必選項:O_RDONLY 只讀O_WRONLY 只寫O_RDWR 讀寫可選項:O_APPEND 追加O_CREAT 創建文件O_EXCL 與 O_CREAT 一起使用,如果文件存在,則報錯mode 權限位,最終(mode & ~umask)O_NONBLOCK 非阻塞返回值:返回最小的可用文件描述符,失敗返回-1, 設置errno
- close 函數
man 2 close
#include <unistd.h>int close(int fd);
參數介紹: fd為要關閉的文件描述符
返回值:成功返回0, 失敗返回-1, 設置errno
ps:C語言參數使用 | 可以有多項參數的實現原理,實際上就是位符
比如:int 類型 32個位,哪幾個位代表那幾個含義
- read函數
man 2 read
#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);
參數介紹:fd 文件描述符buf 緩沖區count 緩沖區大小
返回值:失敗返回-1,設置errno成功返回讀到的大小0代表讀到文件末尾非阻塞情況下:read返回-1,但是此時需要判斷errno的值,看是否是因為非阻塞的原因導致返回-1
非阻塞情況下:
read返回-1,但是此時需要判斷errno的值,看是否是因為非阻塞的原因導致返回-1 這里回頭再學習以下 ???
代碼示例,使用read函數實現 linux cat命令的功能
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
int main(int arg, char *argv[]) {if(arg != 2) {printf("./a.out filename\n");return -1;}int fd = ope?n(argv[1], O_RDONLY);if(fd == -1) {printf("not found filename\n");return -1;}char buf[256];int read_ret = read(fd, buf, sizeof(buf));while (read_ret > 0){printf("%s", buf);memset(buf, '\0', sizeof(buf));read_ret = read(fd, buf, sizeof(buf)); }close(fd);return 0;
}
注意使用這一行代碼memset(buf, ‘\0’, sizeof(buf));
不然最后一次輸出buf,會出現多余的情況 ,暨每次使用完buf,都清洗一次。
- wirte
#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);
參數介紹:fd 文件描述符buf 緩沖區count 緩沖區大小
返回值:成功,返回寫入的字節數失敗,返回-1,設置errno0, 代表未寫入
- lseek函數
移動文件讀寫位置
#include <sys/types.h>
#include <unistd.h>
off_t lseek(int fd, off_t offset, int whence);
參數介紹:fd文件描述符offset 偏移量whence SEEK_SET 文件開始位置SEEK_CUR 當前位置SEEK_END 結尾返回值:成功:返回當前位置到開始的長度失敗:返回-1,設置errno
以下代碼示例:
將一個字符串helloworld,寫到一個文件里面,讀取出來,輸出在屏幕上
de <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>int main(int argc, char *argv[]) {if(argc != 2) {printf("./a.out filename1");return -1;}int fd = open(argv[1], O_RDWR|O_CREAT, 0666);char *w_temp = "helloworld";int length = strlen(w_temp);int write_ret = write(fd, w_temp, length);if(write_ret>0) {printf("write bytes of %d\n", length);} else {printf("write error\n");return -1;}char r_temp[256];int seek_ret = lseek(fd, 0, SEEK_SET);if(seek_ret == -1) {printf("lseek error\n");return -1;}memset(r_temp, '\0', sizeof(r_temp));int read_ret = read(fd, r_temp, sizeof(r_temp));printf("%d\n", read_ret);if(read_ret !=-1){printf("read bytes of %d\n", strlen(r_temp));printf("read is\n");printf("%s\n", r_temp);} else {printf("read error\n");return -1;}write(STDOUT_FILENO, r_temp, strlen(r_temp));close(fd);return 0;
}
注意
int seek_ret = lseek(fd, 0, SEEK_SET);
將文件讀寫位置,重寫設置為文件開始,不然以后的代碼讀取不到文件的內容。
lseek函數計算文件大小
代碼示例:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>int main(int argc, char *argv[]) {if(argc !=2) {printf("./a.out filename1\n");return -1;}int fd = open(argv[1], O_RDONLY);if(!fd) {printf("open error\n");return -1;}int file_size = lseek(fd, 0, SEEK_END);printf("%s file size is %d\n", argv[1], file_size);close(fd);return 0;
}
lseek 拓展文件
以下代碼,創建一個新文件, 并且使其大小為1024字節。
#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) {printf("./a.out filename1\n");return -1;}int fd = open(argv[1], O_WRONLY|O_CREAT, 0666);int l_ret = lseek(fd, 1023, SEEK_END);char a[1] = {'a'};// 這里必須要寫一次,才能生效int w_ret = write(fd, a, 1); // or write(fd, "a", 1);close(fd);return 0;
}
代碼舉例:
- 同一個進程中,兩次打開同一個文件,進行寫操作
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>int main(int argc, char *argv[]) {if(argc != 2) {printf("./a.out filename1\n");return -1;}int o_ret = open(argv[1], O_RDWR|O_CREAT, 0666);printf("o_ret = %d\n", o_ret);int o_ret1 = open(argv[1], O_RDWR|O_CREAT, 0666);printf("o_ret1 = %d\n", o_ret1);write(o_ret, "hello", 5);lseek(o_ret1, 5, SEEK_CUR);// 這里注意lseek的用法,不然,下面的world,會把上面的hello覆蓋掉write(o_ret1, "world", 5);int cl_ret = close(o_ret);int cl_ret1 = close(o_ret1);printf("cl_ret = %d\n", cl_ret);printf("cl_ret1 = %d\n", cl_ret1);return 0;
}