温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》
  • 首页 > 
  • 教程 > 
  • 开发技术 > 
  • 编程语言 > 
  • 使用write函数来编写一个程序,在程序中指定一个文件,用户可以向程序中一次写入不超过80个字符的数

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

发布时间:2020-07-04 07:20:10 来源:网络 阅读:567 作者:银河星君 栏目:编程语言

/*

  • 使用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;
}

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI