Write and Reread a binary file.

A sample about writing a binary file in C, reread it, using two methods.

fread,fwrite

See version :

Pas de dépendances

Download :

#include <stdio.h>


void ecriture1()
{
    FILE* F;
    int a,b;
    double d;
    printf("-- ecriture1\n");
    F = fopen("test.bin","wb");
    a = 5;
    b = 6;
    d = 5.065;
    fwrite(&a,sizeof(int),1,F);
    fwrite(&d,sizeof(double),1,F);
    fwrite(&b,sizeof(int),1,F);    
    fclose(F);
}

void lecture1()
{
    int a;
    int b;
    double d;
    FILE* F;
    printf("-- lecture1\n");
    F = fopen("test.bin","rb");
    fread(&a,sizeof(int),1,F);
    fread(&d,sizeof(double),1,F);
    fread(&b,sizeof(int),1,F);    
    printf("a = %d ; d = %lf ; b = %d\n",a,d,b);
    fclose(F);
}

int main()
{
    ecriture1();    
    lecture1();    
    return 0;
}



Explanations

	No explanations yet.