How to write a c program to structure writing to open 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", "wb")) == NULL)
{
printf("Error opening file.\n");
exit(1);
}
scanf("%d", &in.number);
getchar();
gets(in.name);
if(fwrite(&in, sizeof in, 1, fp) != 1)
{
printf("Error writing to file\n");
exit(2);
}
fclose(fp);
return 0;
}