Dotcpp  >  编程教程  >  io.h头文件  >  C语言read()函数:用于读取打开文件的内容

C语言read()函数:用于读取打开文件的内容

点击打开在线编译器,边学边练

函数名: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



本文固定URL:https://www.dotcpp.com/course/460

C语言函数库
assert.h头文件
ctype.h头文件
float.h头文件
io.h头文件
math.h头文件
mem.h头文件
setjmp.h头文件
stdio.h头文件
stdlib.h头文件
sigal.h头文件
string.h头文件
time.h头文件
Dotcpp在线编译      (登录可减少运行等待时间)