Read a file (binary) into an array of structs
Hi, I am writing a program that writes data to a file in binary mode and then lets you read it back in but I am having trouble with getting it to read anything from the file using fread().
Code:
void reportStats(struct engineer *engineers) {
FILE *filePtr;
filePtr = fopen("BOCTable.dat", "rb");
char choice;
int i;
printf("Please select the type of report you require\n\n");
printf("\ta\tList of all assigned engineers\n");
printf("\tc\tList of all on-call engineers\n\n");
printf("Please enter your choice: ");
scanf("%c", &choice);
fflush(stdin);
if(choice == 'a') {
fread(engineers, sizeof(struct engineer), 64, filePtr);
for(i=0; i<64; i++) {
if(engineers[i].assignmentState == 'a') {
printf("\n\n%s\n", engineers[i].name);
}
}
}
else if(choice == 'c') {
fread(engineers, sizeof(struct engineer), 64, filePtr);
for(i=0; i<64; i++) {
if(engineers[i].assignmentState == 'c') {
printf("\n\n%s\n", engineers[i].name);
}
}
}
getch();
}
Now for some reason, all that does is go to a new line and print nothing. I have set up a counter variable that is used to write each engineer into the next element of the struct array but for some reason, the counter is showing 2618116 instead of 1/2/3 etc each time I add a new engineer.
If more code is needed, I can provide the whole file that I have so far.