繼續文件操作,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,並考慮一下問題出在哪裏.