write函数

王朝百科·作者佚名  2010-02-18  
宽屏版  字体: |||超大  

类型C语言函数

函数名write

功 能写到一文件中

用 法int write(int handel, void *buf, int nbyte);

handel 是文件描述符;

buf是你指定的缓冲区,即指针,指向一段内存单元;

nbyte是你要写入文件指定的字节数;

返回值:写入文档的字节数(成功);-1(出错)

程序例#include <stdio.h>

#include <stdlib.h>

#include <fcntl.h>

#include <sysstat.h>

#include <io.h>

#include <string.h>

int main(void)

{

int handle; char string[40];

int length, res;/* Create a file named "TEST.$$$" in the current directory and write a string to it. If "TEST.$$$" already exists, it will be overwritten. */

if ((handle = open("TEST.$$$", O_WRONLY | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE)) == -1)

{

printf("Error opening file.

");

exit(1);

}

strcpy(string, "Hello, world!

");

length = strlen(string);

if ((res = write(handle, string, length)) != length)

{

printf("Error writing to the file.

");

exit(1);

}

printf("Wrote %d bytes to the file.

", res);

close(handle); return 0; }

 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝百科 版权所有