#include <stdio.h>
#include <string>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
remove("./log.txt");//先删除记录文件
int fd;
if ((fd = open ( "./log.txt" , O_WRONLY|O_APPEND|O_CREAT,0777)) == -1)
//我们想追加写,所以是O_APPEND
/*如果要创建文件,需要在open函数末尾加入文件权限,0777就是对应chmod指令的用户权限,其实就是如下权限全部相与
S_IRUSR: 读权限,文件属主
S_IWUSR: 写权限,文件属主
S_IXUSR: 执行权限,文件属主
S_IRGRP: 读权限,文件所属组
S_IWGRP: 写权限,文件所属组
S_IXGRP: 执行权限,文件所属组
S_IROTH: 读权限,其它用户
S_IWOTH: 写权限,其它用户
S_IXOTH: 执行权限,其它用户
*/
{
//以只写方式打开文件描述符,
printf( "error: %d\n" , errno);
exit (1);
}
string input_xy="要写的内容"+"\n";
char c[1024];
strcpy(c,input_xy.c_str());//字符串转化为char数组
write(fd,c,strlen(c));//以无缓冲写入
close(fd);
C++ Linux环境下创建文件记录数据
最新推荐文章于 2024-04-10 22:21:30 发布