|
write函数的使用
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <string.h>
- int main(void)
- {
- //打开指定路径(开发板的路径)下的1.txt文件,通过可读可写的权限
- int fd = open("/liangzai/2.txt",O_RDWR);
- if(fd == -1)
- {
- //在冒号后面打印出错的原因
- perror("打开txt文件失败:");
- return -1;
- }
- //定义一个指针,用来保存即将写入的数据
- char *buf = "1.TXT说的对!我也这样觉得.";
- //sizeof()运算符,求指针的大小,在64位系统上,永远都是8个字节
- //在32位系统上,永远都是4个字节
- //读取1.TXT里面的数据
- int ret = write(fd,buf,strlen(buf));
- if(ret == -1)
- {
- perror("写入数据失败:");
- return -1;
- }
- else
- printf("ret = %d\n",ret);
- close(fd);
- return 0;
- }
复制代码
|
|