function_fread.doc

上传人:11****ws 文档编号:3179561 上传时间:2019-05-24 格式:DOC 页数:2 大小:31.50KB
下载 相关 举报
function_fread.doc_第1页
第1页 / 共2页
function_fread.doc_第2页
第2页 / 共2页
亲,该文档总共2页,全部预览完了,如果喜欢就下载吧!
资源描述

1、functionfreadsize_t fread ( void * ptr, size_t size, size_t count, FILE * stream );Read block of data from streamReads an array of count elements, each one with a size of size bytes, from the stream and stores them in the block of memory specified by ptr.The position indicator of the stream is advan

2、ced by the total amount of bytes read.The total amount of bytes read if successful is (size * count).ParametersptrPointer to a block of memory with a minimum size of (size*count) bytes.sizeSize in bytes of each element to be read.countNumber of elements, each one with a size of size bytes.streamPoin

3、ter to a FILE object that specifies an input stream.Return ValueThe total number of elements successfully read is returned as a size_t object, which is an integral data type.If this number differs from the count parameter, either an error occured or the End Of File was reached.You can use either fer

4、ror or feof to check whether an error happened or the End-of-File was reached.Example1234567891/* fread example: read a complete file */#include #include int main () FILE * pFile;long lSize;char * buffer;size_t result;01112131415161718192021222324pFile = fopen ( “myfile.bin“ , “rb“ );if (pFile=NULL)

5、 fputs (“File error“,stderr); exit (1);/ obtain file size:fseek (pFile , 0 , SEEK_END);lSize = ftell (pFile);rewind (pFile);/ allocate memory to contain the whole file:buffer = (char*) malloc (sizeof(char)*lSize);if (buffer = NULL) fputs (“Memory error“,stderr); exit (2);/ copy the file into the buf

6、fer:result = fread (buffer,1,lSize,pFile);if (result != lSize) fputs (“Reading error“,stderr); exit (3);/* the whole file is now loaded in the memory buffer. */ terminatefclose (pFile);free (buffer);return 0;This code loads myfile.bin into a dynamically allocated memory buffer, which can be used to manipulate the content of a file as an array.

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育教学资料库 > 精品笔记

Copyright © 2018-2021 Wenke99.com All rights reserved

工信部备案号浙ICP备20026746号-2  

公安局备案号:浙公网安备33038302330469号

本站为C2C交文档易平台,即用户上传的文档直接卖给下载用户,本站只是网络服务中间平台,所有原创文档下载所得归上传人所有,若您发现上传作品侵犯了您的权利,请立刻联系网站客服并提供证据,平台将在3个工作日内予以改正。