I am facing this peculiar problem where in i am writing to a file using this function fprintf but instead of writng the data to the file i.e. name age and salary only the name gets written to the file but age and salary are instead printed some garbage values.
[insert]
Code:
#include <stdio.h>
#include <stdlib.h>
int main(void){
FILE *fp;
char another = 'y';
char name[40];
int age;
float bs;
fp = fopen("C:\\Documents and Settings\\ROHAN\\Desktop\\DESKTOP\\EMP.dat", "w");
if(fp == NULL){
printf("\n Unable to open the file");
exit(EXIT_FAILURE);
}
while(another == 'y' || another == 'Y'){
printf("\n Enter the name, age and basic salary\n");
scanf("%s %d %f", name, &age, &bs);
fprintf(fp, "%s %d %f\n", name, &age, &bs);
printf("\n Enter another emp");
fflush(stdin);
another = getche();
}
fclose(fp);
return 0;
}
So for example if i were to enter
XYZ 34 80980
XYZX 34 09809870
only
XYZ some garbage value for age and salary
XYZX some garbage value for age and salary
woudl be present in the file.