简体   繁体   中英

Strange text reading from file using fread

Im having problems with the read from a file I write to, it shows strange tokens and not the text I tried to print. It's probably just a simple solution but I can't find it. Been sitting here for hours now! :/

http://imgur.com/syiJCPS

#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>
//#include "allafunktioner.h"

struct vinnare{
    int ar;
    char namn[20];
};
struct vinnare *inlasningTillFil(struct vinnare *vinnare, int antalvinnareinlasning);
int meny();

int vinnarear(int ar, char namnlista, int vinnare);
void artistnamn(int ar, char namnlista, int vinnare);
void skrivutalla(int ar, int  namnlista, int vinnare);

main(){
    int a=1, val, antalvinnareinlasning,test=0;

    struct vinnare *vinnare;

    FILE *file;
    file = fopen("F:\\Uppgifter", "rb");

    if (file == NULL){

        vinnare = NULL;
        printf("Ange antal vinnare:");
        scanf("%d", &antalvinnareinlasning);
        vinnare = (struct vinnare *)malloc(antalvinnareinlasning*sizeof(struct vinnare));
        vinnare = inlasningTillFil(vinnare, antalvinnareinlasning);
    }
    else{

        fread(&antalvinnareinlasning, sizeof(int), 1, file);
        printf("%d", antalvinnareinlasning);
        vinnare =(struct vinnare *)malloc(antalvinnareinlasning*sizeof(struct vinnare));

        for (a = 1; a < (antalvinnareinlasning + 1); a++){
            fread(&vinnare[a].ar, sizeof(int), 1, file);
            fread(&vinnare[a].namn, sizeof(char)*20, 1, file);
            printf("%d", vinnare[a].ar);
            printf("%s", vinnare[a].namn);
        }


    }
    fflush(stdin);
    getchar();
}


struct vinnare *inlasningTillFil(struct vinnare *vinnare, int antalvinnareinlasning){
    int a, temp;

    FILE *file;
    file = fopen("F:\\Uppgifter", "wb");

    vinnare[0].ar = antalvinnareinlasning;


    fwrite(&vinnare[0], sizeof(struct vinnare),1, file);


    for (a = 1; a < (antalvinnareinlasning + 1); a++){
        printf("Ange vilket år du vill mata in: ");
        scanf("%d", &temp);
        vinnare[a].ar = temp;
        fflush(stdin);

        printf("Ange vinnaren för det året:");
        fgets(vinnare[a].namn, 20, stdin);
        fflush(stdin);

        fwrite(&vinnare[a], sizeof(struct vinnare), 1, file);
    }

}

When you first read the file in, you read an int for the number of records.

fread(&antalvinnareinlasning, sizeof(int), 1, file);

But when you write the file the first thing you do is write an uninitialized struct vinnare :

fwrite(&vinnare[0], sizeof(struct vinnare),1, file);

So your file format doesn't line up and you end up with binary data from int in your char field.

Also, I would recommend that you do your fread exactly the same as your fwrite in case there is padding in your structure. sizeof(struct vinnare) in not guarenteeded to be exactly the same as sizeof(int) + sizeof(char[20]) , therefore:

fread(&vinnare[a].ar, sizeof(int), 1, file);
fread(&vinnare[a].namn, sizeof(char)*20, 1, file);

is not that same necessarily as

fread( &( vinnare[a]), sizeof(struct vinnare), 1, file) ;

Finally, you allocate N records in memory to store the struct , but you use the wrong ones. You need to index 0 through N-1 not 1 to N in your for loop in inlasningTillFil . The last one read will be at location &vinnare[N] which is not valid.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM