David Lopes David Lopes - 2 months ago 17
C Question

The program stops working when I'm trying to load data from a file

First of all, i just want to make sure you know I'm still studying the language C.
I'm trying to create a program that manages data from a file, but I'm failing at retrieving data from it. Perhaps my logic is incorrect, or perhaps I'm not using the functions correctly.

-The program is not fully implemented

-The error occurs ( the program stops ) when the user inserts '2'.

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

#define FICH "dados.bin"

typedef struct data
{
char first_name[15+1];
char last_name[15+1];
short int age;
int phone_number;
int ID_number;
} DADOS;

char *Main_menu[] = { //MAIN MENU
"1. Insert Data",
"2. Change Data",
"3. Delete Data",
"4. List Data",
"5. Search",
"0. Leave",
NULL
};

char *Sub_menu[] = { //Secundary menu
"1. Search by age",
"2. Search by name",
"0. Back",
NULL
};

FILE *fp; //FILE POINTER

/***********************************************************/
/*Opens the file, or creates it */
/* */
/***********************************************************/
void inic ()
{
fp = fopen(FICH, "r+b");
if (fp == NULL)
{
fp = fopen (FICH, "w+b");
if( fp == NULL)
{
fprintf(stderr, "\nCould not create the file!\n\n");
exit(1);
}
}
}

/********************************************/
/*Shows the main table and the secundary one*/
/********************************************/
void Display_table(int x)
{
int i;
if (x == 1)
{
for (i=0; i<6; i++)
printf("%s\n", Main_menu[i]);
}
else
for (i=0; i<3; i++)
printf("%s\n", Sub_menu[i]);
}

/******************************************************/
/*Display, and insertion of the data */
/******************************************************/

void Insert_reg ()
{
DADOS user1;
printf("\nFirst name: ");
scanf("%s", &user1.first_name);
fwrite(user1.first_name, sizeof(user1), 15+1, fp);
printf("\nLast name: ");
scanf("%s", &user1.last_name);
fwrite(user1.last_name, sizeof(user1), 15+1, fp);

fflush(stdin);

printf("\nAge: ");
scanf("%sd", &user1.age);
fwrite(&user1.age, sizeof(user1), 3, fp);

printf("\nPhone number: ");
scanf("%d", &user1.phone_number);
fwrite(&user1.phone_number, sizeof(user1), 1, fp);

printf("\nID number: ");
scanf("%d", &user1.ID_number);
fwrite(&user1.ID_number, sizeof(user1), 1, fp);
}

/**************************************************************/
/*Loads the binary file and saves it on the structure */
/**************************************************************/

void load_reg()
{
DADOS user1;
fread(&user1.first_name, sizeof(user1), 15+1, fp);
fread(&user1.last_name, sizeof(user1), 15+1, fp);
fread(&user1.age, sizeof(user1), 1, fp);
fread(&user1.phone_number, sizeof(user1), 1, fp);
fread(&user1.ID_number, sizeof(user1), 1, fp);
}

void show_reg()
{
DADOS user1;
printf("\nName: %s %s\n", user1.first_name, user1.last_name);
printf("Age: %d\n", user1.age);
printf("Phone number: %d\n", user1.phone_number);
printf("ID number: %d\n", user1.ID_number);

}
/*************/
/*FUNÇÃO MAIN*/
/*************/

int main (void)
{
short Menu_num;
inic();
Display_table(1); //Main table
scanf("%d", &Menu_num);

while (Menu_num < 0 || Menu_num >5)
printf("\nInsert a valid menu!\n");

switch (Menu_num)
{
case 1: Insert_reg (); break;
case 2: load_reg(); show_reg();
}

}

Answer

fread is badly used. The second parameter should be the size of the data pointed by the first parameter. The third should be the number of datum you want to read, so your call should looks like : fread(&user1.first_name,sizeof(user1.first_name),1,fp);.

All could have been read by a single call : fread(&user1,sizeof(user1),1,fp);.

Are you sure your data are in binary form in the file ?

show_reg() can't display the data you read, as the user1 structure is declared locally to the function. Beware that local variables are not initialized, so they are random filled with garbage.

Declare user1 in the main and modify show_reg and load_reg so that they could be called as : load_reg(&user1) and show_reg(&user1) (transmit it as parameter to the functions).