请输入您要查询的百科知识:

 

词条 write函数
释义

类型

C语言函数

函数名

write

功 能

写到一文件中

用 法

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

handel 是文件描述符;

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

nbyte是你要写入文件指定的字节数;返回值:写入文档的字节数(成功);-1(出错)

write函数把buf中nbyte写入文件描述符handel所指的文档,成功时返回写的字节数,错误时返回-1.

程序例

#include <stdio.h>

#include <stdlib.h>

#include <fcntl.h>

#include <sys\\stat.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; }

随便看

 

百科全书收录4421916条中文百科知识,基本涵盖了大多数领域的百科知识,是一部内容开放、自由的电子版百科全书。

 

Copyright © 2004-2023 Cnenc.net All Rights Reserved
更新时间:2025/3/4 19:23:38