文件IO函数实现
代码1:打开文件并关闭
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>int main()
{int fb = open("./1.txt", O_RDONLY); // 1 打开文件1.txt(只读方式)if (-1 == fb) // 2 判断文件是否打开成功{printf("open error\n");return -1;}printf("fb = %d\n", fb); // 3 打印文件描述符close(fb); // 4 关闭文件return 0; // 5 程序结束
}
核心逻辑:演示 open
和 close
的基本用法,获取文件描述符并关闭文件。
代码2:写文件并在终端输出
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>int main()
{int fb = open("1.txt", O_WRONLY | O_CREAT | O_TRUNC, 0664); // 1 以只写方式打开文件,不存在则创建,清空原内容if (-1 == fb) // 2 判断文件是否打开成功{printf("open error\n");}char s[32] = "Hello world"; // 3 定义字符串数组char *ptr = "Chian"; // 4 定义字符串指针write(fb, s, 11); // 5 写入前11个字节"Hello world"write(fb, ptr, strlen(ptr)); // 6 写入"Chian"write(1, s, strlen(s)); // 7 向标准输出(fd=1)打印"Hello world"close(fb); // 8 关闭文件return 0; // 9 程序结束
}
核心逻辑:演示 write
写文件和直接向标准输出设备写数据。
代码3:一次性读取文件(cat简易版)
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>int main(int argc, char const *argv[])
{if (argc < 2) // 1 检查命令行参数{printf("Usage: ./a.out <srcfile>\n");return -1;}int fb = open(argv[1], O_RDONLY); // 2 只读方式打开文件if (-1 == fb) // 3 判断是否打开成功{printf("open error\n");return -1;}char buffer[1024] = {0}; // 4 定义缓冲区ssize_t cnt = read(fb, buffer, sizeof(buffer)); // 5 读取文件数据printf("%s\n", buffer); // 6 输出到标准输出close(fb); // 7 关闭文件return 0; // 8 程序结束
}
核心逻辑:一次性读取文件内容并输出,适合小文件。
代码4:循环读取文件(cat完整版)
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>int main(int argc, char const *argv[])
{if (argc < 2) // 1 检查命令行参数{printf("Usage: ./a.out <srcfile>\n");return -1;}int fb = open(argv[1], O_RDONLY); // 2 只读方式打开文件if (-1 == fb) // 3 判断是否打开成功{printf("open error\n");return -1;}char buffer[1024] = {0}; // 4 定义缓冲区while (1) // 5 循环读取文件{ssize_t cnt = read(fb, buffer, sizeof(buffer)); // 6 每次读取最多1024字节if (0 == cnt) // 7 文件末尾退出{break;}write(1, buffer, cnt); // 8 输出到终端}close(fb); // 9 关闭文件return 0; // 10 程序结束
}
核心逻辑:适用于大文件的 cat
实现,分块读取,避免一次性读入过多内容。
代码5:文件拷贝功能
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>int main(int argc, char const *argv[])
{if (argc < 3) // 1 检查参数数量{printf("Usage: ./a.out <srcfile> <destfile>\n");return -1;}int srcfb = open(argv[1], O_RDONLY); // 2 打开源文件int desfb = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0644); // 3 打开目标文件(不存在则创建)if (-1 == srcfb || -1 == desfb) // 4 判断文件是否成功打开{printf("open error\n");return -1;} char buffer[1024] = {0}; // 5 定义缓冲区while (1) // 6 循环读取并写入{ssize_t cnt = read(srcfb, buffer, sizeof(buffer)); // 7 从源文件读取if (0 == cnt) // 8 读取结束{break;}write(desfb, buffer, cnt); // 9 写入到目标文件}close(srcfb); // 10 关闭源文件close(desfb); // 11 关闭目标文件return 0; // 12 程序结束
}
核心逻辑:用文件IO实现 cp
命令的功能。
代码6:文件定位与修改(lseek示例)
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>int main()
{int fb = open("./1.txt", O_RDONLY | O_TRUNC | O_WRONLY); // 1 以写方式打开文件并清空内容if (-1 == fb) // 2 判断是否打开成功{printf("open error\n");return -1;}off_t len = lseek(fb, 10, SEEK_SET); // 3 将文件位置移动到第10个字节write(fb, "A", 1); // 4 在该位置写入 'A'printf("%ld\n", len); // 5 打印当前位置len = lseek(fb, -3, SEEK_CUR); // 6 从当前位置向前移动3字节write(fb, "B", 1); // 7 写入 'B'printf("%ld\n", len); // 8 打印当前位置len = lseek(fb, 2, SEEK_END); // 9 从文件末尾向后移动2字节write(fb, "C", 1); // 10 写入 'C'printf("%ld\n", len); // 11 打印当前位置len = lseek(fb, 0, SEEK_END); // 12 移动到文件末尾printf("%ld\n", len); // 13 打印文件长度lseek(fb, 0, SEEK_SET); // 14 回到文件开头close(fb); // 15 关闭文件return 0; // 16 程序结束
}
核心逻辑:演示 lseek
的三种模式(SEEK_SET
、SEEK_CUR
、SEEK_END
)的使用,以及如何随机访问文件位置进行修改。