linux文件系统和操作方法
Linux文件系统的结构如下:
Linux文件操作方式主要通过文件描述符(fd)进行底层文件操作(系统调用)以及使用I/O库函数。
Linux底层文件操作(关于文件的系统调用)
write函数的使用:
成功:返回实际写入的字节数
失败:返回-1,并设置错误号errno,可使用strerror(errno)查看错误信息
注意:从文件当前指针位置开始写入,文件刚打开时指针指向文件头。
示例:
#include <stdlib.h> #include <stdio.h> #include <errno.h> #include <string.h> int main(void) { char buff[] = "hello world\n"; //1 2 都是输出到控制台 int len = 0; len = write(1, buff, sizeof(buff)); //标准输出 if (len != -1) { printf("写入成功,写入的字节数为:%d\n", len); } else { printf("写入失败,错误信息为:%s\n", strerror(errno)); } return 0; }
read函数的使用:
返回值大于0——实际读取的字节数
返回值为0——已读到文件尾
返回值为-1——出错
注意:参数3表示最多能接受的字节数,而不是指一定要输入的字节数。
示例:
char buffer[1024]; int cnt = read(0, buffer, sizeof(buffer)); //从标准输入读 if (cnt > 0) { buffer[cnt] = '\0'; //确保字符串以空字符结尾 printf("读取成功,读取的字节数为:%d,内容为:%s\n", cnt, buffer); } else if (cnt == 0) { printf("已读到文件尾\n"); } else { printf("读取失败,错误信息为:%s\n", strerror(errno)); }
open函数的使用:
int open(const char *pathname, int flags); int open(const char *pathname, int flags, mode_t mode);
成功:返回文件描述符
失败:返回-1
打开方式:
第三个参数:设置权限(略)
注意:返回的文件描述符是该进程未打开的最小的文件描述符。
示例:
#include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <errno.h> #include <string.h> #define FILE_RW_LEN 1024 int main(void) { //第二个参数-文件存在则无法打开 //O_APPEND —— 追加 int fd = open("./test_open.txt", O_CREAT | O_EXCL | O_RDWR, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH); int count = 0; char buffer[FILE_RW_LEN] = "hello i am test"; if (fd != -1) { count = write(fd, buffer, strlen(buffer)); if (count != -1) { printf("写入成功,写入的字节数为:%d\n", count); } else { printf("写入失败,错误信息为:%s\n", strerror(errno)); } close(fd); } else { printf("打开文件失败,错误信息为:%s\n", strerror(errno)); } return 0; }
close函数的使用:
int close(int fd);
成功:返回0
失败:返回-1
示例:
#include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <errno.h> #include <string.h> #define FILE1_NAME "file1.txt" #define FILE2_NAME "file2.txt" int main(void) { int file1, file2; char buffer[4096]; int len = 0; file1 = open(FILE1_NAME, O_RDONLY); if (file1 != -1) { file2 = open(FILE2_NAME, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR); if (file2 != -1) { while ((len = read(file1, buffer, sizeof(buffer))) > 0) { write(file2, buffer, len); } close(file2); //实战记得判断是否关闭成功 } else { printf("无法打开文件%s,错误信息为:%s\n", FILE2_NAME, strerror(errno)); } close(file1); } else { printf("无法打开文件%s,错误信息为:%s\n", FILE1_NAME, strerror(errno)); } return 0; }
观察耗时:
time xxx 程序总的执行时间
程序本身所消耗的时间
系统调用所消耗的时间
lseek函数的使用:
off_t lseek(int fd, off_t offset, int whence);
成功:返回新的文件位置与文件头之间的偏移
失败:返回-1
示例:从一个文件偏移100处,拷贝100字节到另一个文件。
#include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <errno.h> #include <string.h> #define FILE1_NAME "lseek_test.c" #define FILE2_NAME "lseek_test2.txt" #define SIZE 100 int main(void) { int file1, file2; char buffer[1024]; int ret; file1 = open(FILE1_NAME, O_RDONLY); if (file1 != -1) { file2 = open(FILE2_NAME, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR); if (file2 != -1) { lseek(file1, 100, SEEK_SET); // 移动文件指针到偏移100处 ret = read(file1, buffer, SIZE); // 读取100字节 if (ret > 0) { buffer[ret] = '\0'; write(file2, buffer, SIZE); } close(file1); close(file2); } else { printf("无法打开文件%s,错误信息为:%s\n", FILE2_NAME, strerror(errno)); } } else { printf("无法打开文件%s,错误信息为:%s\n", FILE1_NAME, strerror(errno)); } return 0; }
ioctl函数的使用(嵌入式相关):
int ioctl(int fd, int cmd, [int *argdx, int argcx]);
fd是用户程序打开设备时使用open函数返回的文件标识符,cmd是用户程序对设备的控制命令,后面是一些补充参数,一般最多一个,这个参数的有无和cmd的意义相关。
以上就是【Linux】Linux文件的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号