使用write函数来编写一个程序,在程序中指定一个文件,用户可以向程序中一次写入不超过80个字符的数

/*

  • 使用write函数来编写一个程序,在程序中指定一个文件,用户可以向程序中一次写入不超过80个字符的数据。
    */
    #include <sys/types.h>
    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <string.h>

#define FLAGS O_RDWR|O_APPEND
#define SIZE 80
#define FILENAME "/home/mrhe/hello"

int main(int argc, char argv[])
{
int count;
int fd;
char write_buf[SIZE];
const char
pathname=FILENAME;
if((fd=open(pathname,FLAGS))==-1)//调用open函数打开文件
{
printf("error,open file failed!\n");
exit(1);
}
printf("OK,open file successful!\n");
printf("Begin write:\n");
gets(write_buf);
count=strlen(write_buf);
if(write(fd,write_buf,count)==-1)//要写入文件的数据的字节数
{
printf("error,write file failed!\n");
exit(1);
}
printf("OK,write %d strings to file!\n",count);
return 0;
}