写文件(C)

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<unistd.h>
 4 #include<string.h>
 5 #include<errno.h>
 6 #include<sys/types.h>
 7 #include<sys/stat.h>
 8 #include<fcntl.h>
 9 
10 int main(int arg, char *args[])
11 {
12 
13 //    char s[]="abc.txt";
14     int fd = open(args[1],O_RDWR|O_APPEND);//用读写追加的方式打开文件
15     if(fd==-1)
16         printf("err id %s
",strerror(errno));
17     else
18     {
19         printf("success fd =%d
",fd);
20         char buf[100];
21         memset(buf,0,sizeof(buf));
22         strcpy(buf,"hello world
");
23         int i=write(fd,buf,strlen(buf));//这里要用strlen函数
24         close(fd);
25 
26     }
27     return 0;
28 }
原文地址:https://www.cnblogs.com/leejxyz/p/5688526.html