函数名
:read
头文件
:<io.h>
函数原型
: int read(int handle,void *buf,int len);
功能
:用于读取打开文件的内容
参数
:int handle 为要读取的文件
void *buf 为要将读取的内容保存的缓冲区
int len 读取文件的长度
返回值
:返回实际读取的字节数
程序例
:创建文件,内容为 I like www.dotcpp.com very much!
//打开文件,读取文件的内容
#include<stdio.h>
#include<io.h>
#include<fcntl.h>
int main(void){
int fd=open("D:\\a.txt",O_RDONLY);
if(fd==-1){
printf("can not open the file\n");
return 1;
char buf[1024]={"\0"};
int len=read(fd,buf,1024);
printf("%s\nlen=%d\n",buf,len);
close(fd);
return 0;
}
运行结果
I like www.dotcpp.com very much!
len=32