I'm having a problem with this.
I input some values with spaces and the compiler when I put a space in a string start to input that information to the next field and when i list those items they are all wrong...but in the File they are in the correct order.
For example, I input:
Name: John Doe
ID: 123
Age: 50
In the txt file will appear
"
John Doe
123
50"
But when i got to list the employees it will appear:
Name: John
ID: Doe
Age: 123
I have this function:
Code:
addEmp{
int i=0;
loadEmployee();
do{
printf("\n\nName:");
scanf(" ");
gets(employee[total_employees].name);
printf("\nID:");
scanf(" ");
gets(employee[total_employees].id);
printf("\nAge:");
scanf(" ");
gets(employee[total_employees].age);
total_employees++;
saveEmployee();
printf("\nrepeat?");
scanf(" %c", &repeat);
}while(repeat == 's')
}
The functions to load and save are:
Code:
loadEmployee(){
int i=0;
file = fopen("emp.txt", "r");
if(file==NULL)
file = fopen("emp.txt", "w");
else{
fscanf(file," %i",&total_employees);
for(i=0;i<total_employees;i++){
fscanf(file," %s", employee[i].name);
fscanf(file," %s", employee[i].id);
fscanf(file," %s", employee[i].age);
}
}
fclose(file);
}
saveEmployee(){
int i=0;
file = fopen("emp.txt", "w");
fprintf(file," %i\n",total_employees);
for(i=0;i<total_employees;i++){
fprintf(file,"%s\n", employee[i].name);
fprintf(file,"%s\n", employee[i].id);
fprintf(file,"%s\n", employee[i].age);
}
fclose(file);
}
Any help?