sukj
sukj

Reputation: 31

C unknown number of structures in file

Similar with this. But what if MAX_BOOKS would be unknown as well?

I want to get number of structures from a file.

My structure:

typedef struct material {
    int mat_cislo;                      
    char oznaceni[MAX_TEXT];            
    char mat_dodavatel[MAX_TEXT];       
    char dodavatel[MAX_TEXT];           
    float cena;                         
    int mat_kusovnik;                   
} MATERIAL;

My code:

void nacist_material() {
FILE* pSoubor;
MATERIAL materialy_pocitadlo;
int i;
int b;
if((pSoubor = fopen(SOUBOR_MATERIAL, "rb")) == NULL ) {
    printf("\nChyba při čtení souboru");
    return;   
}

pocet_zaznamu_materialu = 3;

printf("\n\n===>%d", pocet_zaznamu_materialu);
if(pocet_zaznamu_materialu > 0) {
    printf("\nExistuje %d materialu", pocet_zaznamu_materialu);


    free(pMaterialy);

    pMaterialy = (MATERIAL *) malloc(pocet_zaznamu_materialu * sizeof(MATERIAL));

    for(i = 0; i < pocet_zaznamu_materialu; i++) {
        b = fread(&pMaterialy[i], sizeof(MATERIAL), 1, pSoubor);  
    }
    printf("\n otrava %d", b); 
}
else {
    printf("\nNeexistuje předchozí záznam materialu");
}
fclose(pSoubor);
return;

}

Right now pocet_zaznamu_materialu is hard code to 3, because there are 3 structures in a file and it all works correctly. But what if number of structures in file changes?

Problem: I need to know - number of structures in file, how to a do it?

Thanks, sorry for eng

Upvotes: 1

Views: 362

Answers (3)

Sparky
Sparky

Reputation: 14077

Have you considered adding a header to the file?

That is, place a special structure at the start of the file that tells you some information about the file. Something like ...

struct file_header {
    char      id[32];        /* Let this contain a special identifying string */
    uint32_t  version;       /* version number in case the file structure changes */
    uint32_t  num_material;  /* number of material structures in file */
};

Not only does this give you a relatively quick way to determine how many material structures you have in your file, it is also extensible. Perhaps you will want to store other structures in this file, and you want to know how many of each are in there--just add a new field and update the version.

If you want, you can even throw in some error checking.

Upvotes: 0

Serge Ballesta
Serge Ballesta

Reputation: 149095

This can be a real problem when you read from a dynamic file (another program writes at the end of file while you read it), a pipe or a network socket. In that case, you really have no way to guess the number of structs.

In that case, a common idiom is to use a dynamicaly allocated array of structs of an arbitrary size and then make it grow with realloc each time the currently allocated array is full. You could for example make the new size be twice the previous one.

That is the way C++ vectors manage their underlying array under the hood.

Upvotes: 2

Govind Parmar
Govind Parmar

Reputation: 21562

If the file is composed of nothing but a list of your desired struct stored contiguously, then the file's size, in bytes, will be a multiple of the size of your struct, and you can obtain the file size and then the number of structs in the file like so:

size_t len_file, num_structs;

fseek(fp, 0, SEEK_END);
len_file = ftell(fp);
rewind(fp);

num_structs = len_file/sizeof(MYSTRUCT);

Upvotes: 2

Related Questions