0

I'm trying to print the contents of a binary file, but when I run the code, after inserting the various variables, they don't get printed, and the algorithm just ends. I think the fwrite is correct, but I'm not sure.

This is the code:

typedef struct{
    char descrizione[80];
    float voto;
}materia;

typedef struct{
    char nome[80];
    char cognome[80];
    materia b[2];
}studente;

void lettura(FILE *fp, int n, studente a[n])
{
    int i, j;
    fp=fopen("file.bin","wb");
    for(i=1;i<=n;i++)
    {
        printf("\nInserisci il nome del %d studente: ",i);
        gets(a[i].nome);
        
        printf("Inserisci il cognome del %d studente: ",i);
        gets(a[i].cognome);
        
        for(j=1;j<=2;j++)
        {
            printf("Inserisci la materia %d: ",j);
            gets(a[i].b[j].descrizione);
            
            printf("Inserisci il voto di %s %s in %s: ",a[i].nome,a[i].cognome,a[i].b[j].descrizione);
            scanf("%f",&a[i].b[j].voto);
            getchar();
        }
        fwrite(&a[i],sizeof(studente),1,fp);
    }
    fclose(fp);
}

int main(int argc, char *argv[]) {
    int n=2, i=1;
    studente a[n];
    FILE *fp;
    fp=fopen("file.bin","wb");
    fclose(fp);
    lettura(fp,n,a);
    while(fread(&a[0],sizeof(studente),1,fp)>0)
    {
        printf("%s, %s, %s, %f",a[1].nome,a[i].cognome,a[i].b[i].descrizione,a[i].b[i].voto);
        i++;
    }
    return 0;
}
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
Cu be
  • 1
  • 1
    Stop using `gets()` immediately. It's a dangerous function because you can't specify the buffer size, and it has been removed from the language. – Barmar Feb 07 '22 at 15:57
  • 1
    `while(fread(&a...., fp)` You don't open `fp` for reading. – 001 Feb 07 '22 at 15:58
  • 2
    Array indexes go from 0 to n-1. You're writing outside the array in your loop. – Barmar Feb 07 '22 at 15:58
  • 1
    @JohnnyMopp Even worse, they already closed the file. – Barmar Feb 07 '22 at 15:59
  • `fread(&a[0],...)` --> `fread(&a[i],...)` Also, `int i = 1` --> `int i = 0`. And, `a[1].nome` --> `a[i].nome` – Craig Estey Feb 07 '22 at 16:00
  • 1
    Why do you open and then immediately close the file in `main()`? And why do you then pass this closed file pointer to `lettura()`? – Barmar Feb 07 '22 at 16:00
  • Thanks! I honestly don't know how I missed the fopen, I've been staring at this code for at least 1 hour. – Cu be Feb 07 '22 at 16:03
  • See [Why the `gets()` function is too dangerous to be used — ever!](https://stackoverflow.com/q/1694036/15168) – Jonathan Leffler Feb 07 '22 at 16:06
  • In `main()` you have `fp = fopen("file.bin", "wb"); fclose(fp); lettura(fp, n, a);` — passing a closed file pointer to the function `lettura()` is pointless, especially as the first thing the function does is use `fopen()` to overwrite the passed value. It would be more sensible to pass the file name to the function. The whole `fopen()`/`fclose()` sequence should be removed — you don't even check that the open worked. The `while (fread(&a[0], sizeof(studente), 1, fp) > 0)` that follows this code is using a closed file stream — it won't work. You have to reopen the file for reading. – Jonathan Leffler Feb 07 '22 at 16:10

0 Answers0