继续文件操作,write函数
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define FNAME "./test"
int main(void)
{
int fd;
fd = open(FNAME, O_WRONLY | O_TRUNC);
write(fd, "hello world", strlen("hello world"));
close(fd);
return 0;
}
=========================================================
看函数原型
ssize_t write(int fd, const void *buf, size_t count);
第一个参数,open返回的文件描述符
第二个参数,一段内存空间,从这里读内容,然后写进文件
第三个参数,写多少个字节
返回值是实际写进的字节数,如果是0,那就什么都没写,如果是-1那就出错了。
PS:如果你发现运行了上面的程序,结果什么事都没有发生,请参考上一篇【基础】open,并考虑一下问题出在哪里.