saving into a file in c

72 views Asked by At

the scope of this function is to get inputs and save into a file . simple right ? but i cannot find the file after i input whith the console . please help me !

void addpassenger()
{
    int size = sizeof(struct passenger);
    struct passenger newPassenger;

    printf("Name: ");
    gets(newPassenger.name);

    printf("Surname: ");
    gets(newPassenger.surname);

    printf("ID card No: ");
    gets(newPassenger.idCard);

    printf("Nationality: ");
    gets(newPassenger.Nationality);

    printf("Telephone/Mobile: ");
    scanf("%d",&newPassenger.phone);

    FILE *pt =fopen("passenger.dat","a");
    fwrite(&newPassenger,size,1,pt);
    fclose(pt);

}
1

There are 1 answers

0
Srikanth On

fwrite - second and third arguments you have passed are wrong ..

Second Argument is size - Size in bytes of each element to be written. size_t is an unsigned integral type.

Third argument is count - Number of elements, each one with a size of size bytes. size_t is an unsigned integral type

So your arguments should be

fwrite(&newPassenger,sizeof(int),size,pt);