Linux创建并写入文件
write函数头文件指令:man 2 write
close函数头文件指令:man 2 write
strlen函数头文件指令:man strlen
write(fd,buf,strlen(buf));
close(fd);
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
int main()
{
int fd;
char *buf = "chenlichen hen shuai!";
fd = open("./file1",O_RDWR);
if(fd == -1){
printf("open file1 failed\n");
fd = open("./file1 ",O_RDWR|O_CREAT,0600);
if(fd > 0){
printf("craet file1 success\n");
}
}
printf("open success :fd = %d\n",fd);
write(fd,buf,strlen(buf));
close(fd);
return 0;
}