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

 

词条 read()
释义

函数名: read

功 能: 从文件中读

用 法: int read(int handle, void *buf, int nbyte);

表头文件:#include <unistd.h>

函数说明:read()会把参数handle所指的文件传送nbyte个字节到buf指针所指的内存中。若参数nbyte为0,则read()不会有作用并返回0。返回值为实际读取到的字节数,如果返回0,表示已到达文件尾或无可读取的数据。

程序例:

#include <stdio.h>

#include <io.h>

#include <alloc.h>

#include <fcntl.h>

#include <process.h>

#include <sys\\stat.h>

int main(void)

{

void *buf;

int handle, bytes;

buf = malloc(10);

/*

Looks for a file in the current directory named TEST.$$$ and attempts

to read 10 bytes from it. To use this example you should create the

file TEST.$$$

*/

if ((handle =

open("TEST.$$$", O_RDONLY | O_BINARY, S_IWRITE | S_IREAD)) == -1)

{

printf("Error Opening File\");

exit(1);

}

if ((bytes = read(handle, buf, 10)) == -1) {

printf("Read Failed.\");

exit(1);

}

else {

printf("Read: %d bytes read.\", bytes);

}

return 0;

}

随便看

 

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

 

Copyright © 2004-2023 Cnenc.net All Rights Reserved
更新时间:2024/12/23 21:48:08