Hello.. im having a problem with my code.. when i save a file, then choose another variable and load the file, then try to display it, all that it display are garbage values.. i know the problem is either in function save_file or function load_file.. can anyone please help me..
#include<stdio.h>
#include<string.h>
#include<conio.h>
#define max 20
typedef struct
{
char LN[16];
char FN[24];
char MI;
}name;
typedef struct
{
unsigned long ID;
name n;
char course[8];
int year;
}studrec[max];
void save_file(studrec s, int n)
{
FILE *fp;
char filename[30];
int ctr;
printf("What is the name of the file? ");
flushall();
gets(filename);
if((fp = fopen(filename, "wb")) == 0)
{
printf("Cannot write file");
}
for(ctr = 0; ctr < n; ctr++)
fwrite(&(s[ctr]), sizeof(studrec), 1, fp);
printf("File Saved");
fclose(fp);
getch();
clrscr();
}
void load_file(studrec r)
{
FILE *fp;
int ctr;
char filename[30];
printf("What is the filename? ");
flushall();
gets(filename);
if((fp = fopen(filename, "rb")) == 0)
{
printf("File does not exist");
}
for(ctr = 0; fp == 0; ctr++)
fread(&(r[ctr]), sizeof(studrec), 1, fp);
printf("File Loaded");
getch();
clrscr();
}