- 论坛徽章:
- 0
|
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 |
|