开发者

Read a binary file and store all data into array of structures

开发者 https://www.devze.com 2023-03-13 01:21 出处:网络
I need to write a program which simply a measurement conversion program. The program first ask the user for name of a binary file (unit conversion data), open the file, and set arrays.

I need to write a program which simply a measurement conversion program. The program first ask the user for name of a binary file (unit conversion data), open the file, and set arrays.

My structure:

struct unit {               
    char name[NAME_LEN];        
    char abbrev[ABBREV_LEN];    
    char class[CLASS_LEN];                                      
    double standard;            
};

and my function:

int fread_units(int unit_max,struct unit units[], int *unit_sizep)
{
    FILE *filep;
    struct unit data;
    int i, status;
    char fullname[10];

    /* Gets database of units from file                                 */

    printf("Enter name of binary file> ");
    scanf("%s", fullname);
    strcat(fullname, ".bin");
    i = 0;
    filep = fopen(fullname, "rb");
    //fseek (filep , 0 , SEEK_END);
    for (status = fread(&data, sizeof( struct unit ), 1, filep);
         status == 1 && i < unit_max;
         status = fread(&data, sizeof( struct unit ), 1, filep)){
        units[i++] = data;
    }
    printf("\n%f", units[3].standard);
    /* Issue error message on premature exit                            */
    if (status == 0) {
        printf("\n*** Error in data format ***\n");
        printf("*** Using first %d datavalues ***\n",i);
    }
    else if (status != EOF) {
        printf("\n*** Error: too much data in file ***\n");
        printf("*** Using first %d data values ***\n", i);
    }

    /* Send back size of used portion of array                          */
    *unit_sizep = i;

    if(status == 4)
        status = 1;
    else if (status != EOF)
        status = 0;
    fclose(filep);

    return(status);
}

According to the book, (fread(&data, sizeof( struct unit ), 1, filep);) is enough to store all data into a structure.

But it doesn't work for me...

Output:

*** Error in data format ***
*** Using first 4 datavalues ***
To convert 25 kilometers to miles, you would enter
> 25 kilometers miles
    or, alternatively,
>   25 km mi
> 25 km mi
Attempting conversion of 25.0000 km to mi . . .
Unit kmnot in database

Enter a conversion problem 开发者_运维技巧or q to quit.
> 

my units.bin file;

miles                   mi          distance        1609.3
kilometers              km          distance        1000
yards                   yd          distance        0.9144
meters                  m           distance        1
quarts                  qt          liguid_volume   0.94635
liters                  l           liquid_volume   1
gallons                 gal         liquid_volume   .7854
millimeters             ml          liquid_volume   0.001
kilograms               kg          mass            1
grams                   g           mass            0.001
slugs                   slugs       mass            0.14594


I solved my problem.I created a binary file with following code.

#include <stdio.h>

#define NAME_LEN    30  
#define ABBREV_LEN  15  
#define CLASS_LEN   20  
#define MAX_UNITS   20  

struct unit {               
    char name[NAME_LEN];        
    char abbrev[ABBREV_LEN];    
    char class[CLASS_LEN];                                      
    double standard;            
};

int main(void)
{
    int i;
    struct unit unitp[MAX_UNITS];
    FILE *inp, *outp;
    inp = fopen("units.dat", "r");
    outp = fopen("units.bin", "wb");
    for(i=0;!feof(inp);i++){
        fscanf(inp, "%s%s%s%lf", unitp[i].name,
                                    unitp[i].abbrev,
                                    unitp[i].class,
                                    &unitp[i].standard);
    }
    fwrite(unitp, sizeof(struct unit ), i, outp);

    fclose(inp);
    fclose(outp);

    return(0);
}

and my for loop;

for (status = fread(&units[i++], sizeof( struct unit ), 1, filep);
     i < MAX_UNITS && !feof(filep);
     status = fread(&units[i++], sizeof( struct unit ), 1, filep)){
    //units[i++] = data;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消