zoukankan      html  css  js  c++  java
  • fread

    转自:http://baike.baidu.com/view/656689.htm


    目录

    C语言库函数名:
    展开

    编辑本段C语言库函数名:

    简介

    函数原型:
    size_t fread(void *buffer, size_t size, size_t count, FILE *stream); 
    功 能:
    从一个文件流中读数据,读取count个元素,每个元素size字节.如果调用成功返回count.返回实际读取size*count字节.如不成功,返回实际读取的元素个数
    参 数:
    buffer
    用于接收数据的内存地址,大小至少是 size count 字节.
    size
    单个元素的大小,单位是字节
    count
    元素的个数,每个元素是size字节.
    stream
    输入流,被读取的文件
    返回值:
    实际读取的元素数.如果返回值与count(不是count*size)不相同,则可能文件结尾或发生错误.
    从ferror和feof获取错误信息或检测是否到达文件结尾.

    程序例

    #include <stdio.h>
    #include <string.h>
    int main(void)
    {
    FILE *stream;
    char msg[] = "this is a test";
    char buf[20];
    if ( (stream = fopen("DUMMY.FIL", "w+")) == NULL) {
    fprintf(stderr,"Cannot open output file.\n");
    return 1;
    } /* write some data to the file */
    fwrite(msg, 1,strlen(msg)+1, stream); /* seek to the beginning of the file */
    fseek(stream, 0, SEEK_SET); /* read the data and display it */
    fread(buf, 1,strlen(msg)+1,stream);
    printf("%s\n", buf);
    fclose(stream);
    return 0;
    }

    MSDN示例

    #include <stdio.h>
    void main( void )
    {
    FILE *stream;
    char list[30];
    int i, numread, numwritten; /* Open file in text mode: */
    if( (stream = fopen( "fread.out", "w+t" )) != NULL )
    {
    for ( i = 0; i < 25; i++ )
    list[i] = (char)('z' - i);   /* Write 25 characters to stream */
    numwritten = fwrite( list, sizeof( char ), 25, stream );
    printf( "Wrote %d items\n", numwritten );
    fclose( stream );
    }
    else
    printf( "Problem opening the file\n" );
    if( (stream = fopen( "fread.out", "r+t" )) != NULL )
    { /* Attempt to read in 25 characters */
    numread = fread( list, sizeof( char ), 25, stream );
    printf( "Number of items read = %d\n", numread );
    printf( "Contents of buffer = %.25s\n", list );
    fclose( stream );
    }
    else
    printf( "File could not be opened\n" );
    }

  • 相关阅读:
    SpringMVC—对Ajax的处理(含 JSON 类型)(2)
    md5加密(1)
    js生成邀请码(2)
    SpringMVC---依赖注入与面向切面
    初识json
    java集合类(2)
    springmvc与struts2的区别
    模拟14
    NOIP模拟13
    NOIP模拟12
  • 原文地址:https://www.cnblogs.com/pamxy/p/2991514.html
Copyright © 2011-2022 走看看