fread ライブラリ関数

ストリームからデータを読み出す

size_t fread( void *buffer, size_t size, size_t count, FILE *stream );

ファイル ポインタの現在位置を得る

long ftell( FILE *stream );

指定された位置にファイル ポインタを移動する

int fseek( FILE *stream, long offset, int origin );

サンプルです。

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <malloc.h>

int main()
{
        FILE *fp;
        char *buf;
        size_t readcount, filesize;

        if( (fp = fopen( "test.txt", "r+t" )) != NULL ){
                fseek( fp, 0L, SEEK_END );
                filesize = ftell( fp );
                fseek( fp, 0L, SEEK_SET );

                printf("ファイルサイズ:%dバイト\n", filesize);

                buf = (char*)malloc( filesize );

                readcount = fread( buf, sizeof( char ), (int)filesize, fp );

                buf[readcount] = '\0';

                printf( "ファイル内容\n" );
                printf( "------------------ \n" );
                printf( "%s\n", buf );

                free( buf );
                fclose( fp );
        }else
                printf( "fopen関数失敗\n" );

        getch();

        return 0;
}

トップページ