免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1285 | 回复: 0
打印 上一主题 下一主题

write函数 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-09-20 12:30 |只看该作者 |倒序浏览

  write函数(写入文件)
它的主要功能是:将某个文件缓冲区的数据,写入某个文件内。
系统调用格式:
number = write(handle, buffer, n) ;
write函数各个参数定义如下:
l         handle:  这是一个已经打开的文件句柄,表示将数据写入这个文件句柄所表示的文件内。
l         buffer:  表示缓冲区,也就是把这个缓冲区的数据写入文件句柄所表示的文件内。
l         n:         表示调用一次write操作,应该写如多少字符。
l         number:表示系统实际写入的字符数量。
如果调用write失败,则系统返回-1给number。

Code:
#include "lyl.h"
#define BUF 512
#define PERM 0744
/*本程序会将某个文件内容复制到所指定的文件中。*/
main( int argc,char *argv[])
{
    char buffer[BUF] ;
    int source,dest ;
    int count ;

    if ( argc != 3 )
    {
        printf("sorry input error!\n") ;
        exit(1) ;
    }

    source = open(argv[1],O_RDONLY) ;       /*open source file*/
    if ( source == -1 )
    {
        printf("sorry input error!\n") ;
        exit(1) ;
}

    dest = creat(argv[2],PERM);         /*create dest file*/
    if ( dest == -1 )
    {
        printf("create [%s] file error\n",argv[2]);
        exit(1);
    }

    while( (count = read(source,buffer,BUF)) > 0 )
        write(dest,buffer,count);

    exit(0) ;
}
$more t1.txt
1234567890
$ more t2.txt

$cc -o write write.c ;write t1.txt t2.txt
$more t2.txt
1234567890

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/22541/showart_173937.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP