Home > Net >  Read from binary file
Read from binary file

Time:01-03

I've recieved an assignment in school to create a car register using structs. The register will take some user input in form of name, age, car type, brand, license plate and owner.

The program I've written succesfully writes to the binary file however it doesn't read from it when I want it to.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

#define STARTANTAL 3
#define MAX_SIZE 30
#define SIZE 10
#define VAL_SIZE 5 


typedef struct{
    char namn[MAX_SIZE];
    char alder[MAX_SIZE];
    char fordonstyp[MAX_SIZE];
    char marke[MAX_SIZE];
    char reg[MAX_SIZE];
    char agare[MAX_SIZE];
    
}fordon_t;


void sparatillfil(fordon_t *car, int size);
void lasfil(fordon_t *car, int size);
void laggtill(fordon_t *addcar, int *antal);
void meny();





int main()
{
    int i = 0;
    fordon_t fordon[SIZE];
   int quit = 0;
    
    while(quit == 0){
        
        meny();
   
    char val[VAL_SIZE];
    
    fgets(val,VAL_SIZE, stdin);
        
    
    int tal = atoi(val);
    
     
          
    
    switch(tal){
        case 0:
                sparatillfil(fordon, i);
            return 0;
        
            
        case 1:
            laggtill(fordon, &i);
            
            break;
            
        case 2:
              
            lasfil(fordon, i);
            break;

            
        default:
            printf("VÄLJ ETT AV ALTERNATIVEN\n");
            break;
    }
    
     }
    return 0;
    
}

void sparatillfil(fordon_t *car, int size){
    
    FILE *fp = NULL;
    fp = fopen("register.dat", "wb");
        if(fp == NULL){
            puts("Filen kunde inte läsas\n");
        
        }
    else{
        
        for(int i = 0; i < size; i  ){
            fwrite(&car[i], sizeof(fordon_t), 1, fp);
            
        }
        printf("%d antal bilar har sparats till registret :)\n", size);
    }
    fclose(fp);
}


void laggtill(fordon_t *addcar, int *antal){
    fordon_t bil;
    
    if(*antal < SIZE){
            printf("Namn: \n");
            fgets(bil.namn, sizeof(bil.namn), stdin);
            
            
            printf("Ålder: \n");
            fgets(bil.alder, sizeof(bil.alder), stdin);
       
            
           
            printf("Typ av fordon: \n");
            fgets(bil.fordonstyp, sizeof(bil.fordonstyp), stdin);
        int age = atoi(bil.alder);
        
            
            
            printf("Bilmärke: \n");
            fgets(bil.marke, sizeof(bil.marke), stdin);
            
            printf("REG-nummer: \n");
            fgets(bil.reg, sizeof(bil.reg), stdin);
            
            printf("Ägare: \n");
            fgets(bil.agare, sizeof(bil.agare), stdin);
        
        addcar[*antal] = bil;
        (*antal)  ;
}
   else{
       printf("Registret är fullt\n");
   } 

}

void lasfil(fordon_t *car, int size){
    FILE *fp;
    fp = fopen("register.dat", "ab ");
    
    if(!fp){
        printf("Filen kunde inte läsas in\n");
    }
    else{
        for(int i = 0; i < size;   i){
            fread(&car[i], sizeof(fordon_t), 1, fp);
            
        }
    }
    fclose(fp);
}


void meny(){
    
printf("Välj något av alternativen\n");
    printf("1. Lägg till fordon\n");
    printf("2. Radera ett fordon\n");
    printf("3. Sortera registret\n");
    printf("4. Visa fordonet\n");
    printf("5. Visa registret\n");
    printf("0. Avsluta programmet och spara registret\n");
}

I haven't got a clue of what is wrong unfortunately and debugging the code hasn't been optimal for me since I'm rather new to programming

CodePudding user response:

It looks like there is a problem with the function that reads from the file. Specifically, the file that is being opened for reading is named "reg.txt" while the file that is being written to is named "register.dat".

Try changing the name of the file being opened for reading to "register.dat" and see if that fixes the problem.

Also, in the lasfil() function, you are opening the file in "ab " mode. This mode is used to open a file for both reading and writing. The file is created if it does not exist. However, you are not writing to the file in this function, only reading from it. You should use the "rb" mode instead, which opens a file for reading in binary format.

Try making these changes and see if it fixes the problem.

  • Related