How to write a c program to reading structure from a file in c programming ?
Solution:
#include <stdio.h>
#include <stdlib.h>
struct Info
{
char name[10];
int number;
};
int main()
{
struct Info in;
FILE *fp;
if((fp = fopen("input.txt", "rb")) == NULL)
{
printf("Error opening file.\n");
exit(1);
}
if(fread(&in, sizeof in, 1, fp) != 1)
{
printf("Error reading file\n");
exit(2);
}
fclose(fp);
printf("%d %s\n", in.number, in.name);
return 0;
}