fread & fwrite
来源:互联网 发布:nginx 禁止svn目录 编辑:程序博客网 时间:2024/05/22 00:19
转自:http://www.cnblogs.com/lvpengms/archive/2011/05/01/2033779.html
This artical extracted from www.cplusplus.com.
fread
size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );
Read block of data from stream
Reads 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 postion indicator of the stream is advanced by the total amount of bytes read.
The total amount of bytes read if successful is (size * count).
Parameters
ptr Pointer to a block of memory with a minimum size of (size*count) bytes.
size Size in bytes of each element to be read.
count Number of elements, each one with a size of size bytes.
stream Pointer to a FILE object that specifies an input stream.
Return Value
The 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 ferror or feof to check whether an error happened or the End-of-File was reached.
/* fread example: read a complete file */#include <stdio.h>#include <stdlib.h>int main () { FILE * pFile; long lSize; char * buffer; size_t result; pFile = fopen ( "myfile.bin" , "rb" ); if (pFile==NULL) {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 buffer: 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. */ // terminate fclose (pFile); free (buffer); return 0;}
fwrite
size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream );
Write block of data to stream
Writes an array of count elements, each one with a size of size bytes, from the block of memory pointed by ptr to the current position in the stream.
The postion indicator of the stream is advanced by the total number of bytes written.
The total amount of bytes written is (size * count).
Parameters
ptr Pointer to the array of elements to be written.
size Size in bytes of each element to be written.
count Number of elements, each one with a size of size bytes.
stream Pointer to a FILE object that specifies an output stream.
Return Value
The total number of elements successfully written is returned as a size_t object, which is an integral data type.
If this number differs from the count parameter, it indicates an error.
/* fwrite example : write buffer */#include <stdio.h>int main (){ FILE * pFile; char buffer[] = { 'x' , 'y' , 'z' }; pFile = fopen ( "myfile.bin" , "wb" ); fwrite (buffer , 1 , sizeof(buffer) , pFile ); fclose (pFile); return 0;}
NOTE: the "wb" & "rb" is essential above!
- fwrite 、fread
- fread fwrite
- fread,fwrite
- fread & fwrite
- fread fwrite
- fread,fwrite
- fwrite ,fread
- fread、fwrite
- fread和fwrite
- fread和fwrite
- fread和fwrite
- fread() fwrite() 函数简介
- fread() fwrite() 函数简介
- fread,fwrite,fopen,fclose
- fread-fwrite array
- fread-fwrite pointer
- fread 、fwrite 函数
- vs bug ? fwrite fread
- 正则表达式的用法
- 下拉刷新 & SBJson 的使用
- xUtils框架的介绍(三)
- Android中SQLite应用详解
- 函数地址操作
- fread & fwrite
- Java SE 中map集合详解
- 页面关闭事件
- 在windbg中测试shadow ssdt , win32k!NtUserGetForegroundWindow , hook shadow ssdt
- java char类型 研究
- Java中获取键盘输入值的三种方法
- Arm Linux交叉编译和连接过程分析(1)
- 为动态创建的LinkButton加Onclick事件
- C指针原理(7)-C内嵌汇编