马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
c语言 对文件操纵
1、打开./ipc_test/dev.txt文件,在第1行 覆盖写入"MAC=1q23456789"
代码
- #include <fcntl.h>
- #include <unistd.h>
- #include <string.h>
- #include <stdio.h>
- int main() {
- const char *line_1 = "MAC=1q23456789\n"; // 要写入的内容(含换行符)
- int fd;
-
- // 打开文件(覆盖写入模式,若文件不存在则创建)
- /***
- C 语言中,open 函数是用于打开文件的系统调用,主要用于 低级文件操作。
- 它定义在头文件 <fcntl.h> 中
- (有时也需要 <unistd.h> 进行后续操作如 read、write 和 close)
- **/
- // 如果出错,open 返回 -1,并设置 errno。
- fd = open("./ipc_test/dev.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
-
- if (fd == -1)
- {
- perror("open ./ipc_test/dev.txt failed");
- return -1;
- }
-
- // 写入内容
-
- /***write函数原型ssize_t write(int fd, const void *buf, size_t count);
-
复制代码 |