reading and writing to a binary file
Hey people, i've followed the tutorial on how to do file I/O to binary files however i need one more line, but im not sure what to type in :(
its supposed to open the binary file binFile for reading and writing. then w ite each student record into binFile and therefore read the specified student record from the binary file into the temporary variable tempStudent.
Code:
#include<stdlib.h>
#include<stdio.h>
struct student
{
char name[200];
float marks;
};
int main()
{
FILE * txtFile;
struct student tempStudent;
// File pointer to binary file
FILE * binFile;
int searchNum;
if ((txtFile = fopen("input.txt", "r")) == NULL)
{
printf("Can not open file input.txt\n");
}
else
{
binFile = fopen("binary_file.txt", "wb");
}
while (fscanf(txtFile,"%s %f", tempStudent.name, &(tempStudent.marks)) == 2)
{
// write code that stores the structure tempStudent into the binary file
}
printf("Please enter the student you want to search for\n");
printf("For example if you want the first student type 1\n");
scanf("%d", &searchNum);
fseek(binFile, searchNum*sizeof(tempStudent), SEEK_SET);
fread(???, sizeof(tempStudent), 1, binFile);
printf("The student name retreived is: %s\n", tempStudent.name);
printf("The student mark retreived is: %.2f\n", tempStudent.marks);
fclose(binFile);
fclose(txtFile);
return 0;
}