• fread & fwrite


    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! 

  • 相关阅读:
    帝国 标签模板 使用程序代码 去除html标记 并 截取字符串
    iis6 伪静态 iis配置方法 【图解】
    您来自的链接不存在 帝国CMS
    帝国cms Warning: Cannot modify header information headers already sent by...错误【解决方法】
    .fr域名注册 51元注册.fr域名
    帝国网站管理系统 恢复栏目目录 建立目录不成功!请检查目录权限 Godaddy Windows 主机
    星外虚拟主机管理平台 开通数据库 出现Microsoft OLE DB Provider for SQL Server 错误 '8004' 从字符串向 datetime 转换失败
    ASP.NET 自定义控件学习研究
    CSS层叠样式表之CSS解析机制的优先级
    ASP.NET程序员工作面试网络收藏夹
  • 原文地址:https://www.cnblogs.com/lvpengms/p/2033779.html
Copyright © 2020-2023  润新知