0

I stored data in the binary file with the fwrite() function and the fread() function to read structure from binary file but I got

*** stack smashing detected ***: terminated Aborted (core dumped)

This is my code :

#include <stdio.h>
# define N 3

typedef struct {
    int age;
    char nom[30];
    char prenom[30];
}pers;

void remplir_bin_n(FILE *fi,int n) {
    pers p;
    int i;
        for (i=0;i<n;i++){
            printf("veillez entrez les information du personne n° %d\n", i + 1);
                printf("\tAge: ");
                scanf("%d", & p.age);
                printf("\tNom: ");
                while ((getchar()) != '\n');
                scanf("%[^\n]", p.nom);
                printf("\tPrenom: ");
                while ((getchar()) != '\n');
                scanf("%[^\n]", p.prenom);
            fwrite(&p,3,sizeof(pers),fi);
        }
    fclose(fi);
}

void afficher_file_bin(FILE *f){
    pers pi;
    int i=1;
    fread(&pi,3,sizeof(pers),f);
    while(!feof(f)){
        printf("Information du personne n° %d\n", i );
        printf("\tL'age est : %d\n",pi.age);
        printf("\tLe nom est : %s\n",pi.nom);
        printf("\tLe prenom est : %s\n",pi.prenom);
        fread(&pi,3,sizeof(pers),f);
        i++;
    }
    fclose(f);
}

void main (){
    FILE *f,*fi;
    f=fopen("pers.dat","rb");
    fi=fopen("pers.dat","wb");

    remplir_bin_n(fi,N);
    afficher_file_bin(f);
}
Rachid K.
  • 4,490
  • 3
  • 11
  • 30
taha-ba
  • 11
  • 1
  • 3
    `fread(&pi,3,sizeof(pers),f)` - you are reading 3 items into a space for only one. – Eugene Sh. Mar 02 '21 at 16:52
  • 1
    And similarly for `fwrite(&p,3,sizeof(pers),fi);`. – Ian Abbott Mar 02 '21 at 16:53
  • Not clear to me why it should be `3` at all. – Eugene Sh. Mar 02 '21 at 16:54
  • 1
    Don't abuse `feof` like that: https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – William Pursell Mar 02 '21 at 16:57
  • Although it seems to work, that `feof` loop is not idiomatic. And it will turn into an infinite loop on a read error. – William Pursell Mar 02 '21 at 16:58
  • 1
    regarding; `void main (){` There are only two valid signatures for `main()` they are: `int main( void )` and `int main( int argc, char *argv[] )` – user3629249 Mar 02 '21 at 17:13
  • regarding: `scanf("%[^\n]", p.nom);` always use a MAX CHARACTERS modifier that is 1 less than the length of the input field to avoid any buffer overflow (the `[]` and `s` input format specifiers always append a NUL byte to the input.) suggest: scanf("%29[^\n]", p.nom); – user3629249 Mar 02 '21 at 17:21
  • 1
    [why never use: while( feof(f) )](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – user3629249 Mar 02 '21 at 17:26
  • regarding: `fwrite(&p,3,sizeof(pers),fi);` The code is only writing a single `per` struct at a time so this should be: `fwrite(&p,1,sizeof(pers),fi);` similar considerations exist when reading the struct from the file – user3629249 Mar 02 '21 at 17:29

1 Answers1

0

in the fread and fwrite i used 3 as number of elements in the structure what i missed is that the age , nom and prenom are all in the same structure pers so i had to put 1 in the fread and fwrite so i read the structures data in one variable and everything worked fine without errors .

although thanks for your answers .

taha-ba
  • 11
  • 1