上一讲写到Linux环境下文件的创建,这讲承上启下
- 先看代码
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include<fcntl.h>
#include<stdio.h>
#include<string.h>
int main()
{ int fd; char *buf="LLP IS MY WIEF!"; fd=open("./qqq.c",O_RDWR); if(fd==-1) { printf("open file failed!\n"); fd=open("./qqq.c",O_RDWR|O_CREAT,0600); if(fd>=0) { printf("creat file success!\n"); } } printf("open success!,fd=%d\n",fd); write(fd,buf,strlen(buf)); close(fd); return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 这里write函数的固定格式为
write(int fd,const void * buf,size_t count)
. - 运行结果为
然后打开qqq.c文件
如果我们在Linux环境下不知道某个函数需要什么头文件我们可以用man函数进行查询
man 2 + 函数名
- 1
文章来源: blog.csdn.net,作者:未 央,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/qq_43482790/article/details/115044334