0

Hello, I've been having problems with my program. I really don't get files that much. This is my code:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>

#define max 3

typedef struct
{
	 char FName[24];
	 char LName[16];
	 char MI;
}name;

typedef struct
{
	 unsigned long ID;
	 name n;
	 char course[8];
	 int level;
}profile;

typedef struct
{
	 profile *p;
	 int last;
}student_record, *srptr;

void initialize(srptr *s);
void insert(srptr *s);
void display(srptr s);
void write_file(srptr s);
void read_file(srptr *s);

void initialize(srptr *s)
{
	 *s = (srptr) malloc(sizeof(student_record));
	 (*s)->p = (profile *) malloc(sizeof(profile) * max);
	 (*s)->last = -1;
}

void insert(srptr *s)
{
	 strcpy((*s)->p[++(*s)->last].n.FName, "Bradd");
	 strcpy((*s)->p[(*s)->last].n.LName, "Pitt");
	 (*s)->p[(*s)->last].n.MI = 'P';
	 (*s)->p[(*s)->last].ID = 7000000;
	 strcpy((*s)->p[(*s)->last].course, "BSIT");
	 (*s)->p[(*s)->last].level = 3;

	 strcpy((*s)->p[++(*s)->last].n.FName, "Angelina");
	 strcpy((*s)->p[(*s)->last].n.LName, "Jolie");
	 (*s)->p[(*s)->last].n.MI = 'M';
	 (*s)->p[(*s)->last].ID = 7111111;
	 strcpy((*s)->p[(*s)->last].course, "BSICT");
	 (*s)->p[(*s)->last].level = 1;

	 strcpy((*s)->p[++(*s)->last].n.FName, "Tom");
	 strcpy((*s)->p[(*s)->last].n.LName, "Cruise");
	 (*s)->p[(*s)->last].n.MI = 'A';
	 (*s)->p[(*s)->last].ID = 7222222;
	 strcpy((*s)->p[(*s)->last].course, "ACTMT");
	 (*s)->p[(*s)->last].level = 2;
}

void display(srptr s)
{
	 int ctr;

	 for(ctr = 0; ctr < max; ctr++)
	 {
		  printf("%d)\n", ctr + 1);
		  printf("%s %c. %s\n", s->p[ctr].n.FName, s->p[ctr].n.MI, s->p[ctr].n.LName);
		  printf("%li\n", s->p[ctr].ID);
		  printf("%s %d\n", s->p[ctr].course, s->p[ctr].level);
		  printf("\n");
	 }
}

void write_file(srptr s)
{
	 FILE *fp;
	 int ctr;

	 if((fp = fopen("test.dat", "wb")) == NULL)
		  printf("Cannot open file\n");

	 for(ctr = 0; ctr <= s->last; ctr++)
	 {
		  fwrite(&s->p[ctr].ID, sizeof(long), 1, fp);
		  fwrite(&s->p[ctr].n.FName, sizeof(char), strlen(s->p[ctr].n.FName), fp);
		  fwrite(&s->p[ctr].n.LName, sizeof(char), strlen(s->p[ctr].n.LName), fp);
		  fwrite(&s->p[ctr].n.MI, sizeof(char), 1, fp);
		  fwrite(&s->p[ctr].course, sizeof(char), strlen(s->p[ctr].course), fp);
		  fwrite(&s->p[ctr].level, sizeof(int), 1, fp);
	 }
	 printf("File Saved\n");
	 getch();
	 clrscr();
	 fclose(fp);
}

void read_file(srptr *s)
{
	 FILE *fp;
	 int ctr;

	 if((fp = fopen("test.dat", "rb")) == 0)
		  printf("File is empty\n");

	 for(ctr = 0; ctr < max; ctr++)
	 {
		  fread(&(*s)->p[ctr].ID, sizeof(long), 1, fp);
		  fread(&(*s)->p[ctr].n.FName, sizeof(char), strlen((*s)->p[ctr].n.FName), fp);
		  fread(&(*s)->p[ctr].n.LName, sizeof(char), strlen((*s)->p[ctr].n.LName), fp);
		  fread(&(*s)->p[ctr].n.MI, sizeof(char), 1, fp);
		  fread(&(*s)->p[ctr].course, sizeof(char), strlen((*s)->p[ctr].course), fp);
		  fread(&(*s)->p[ctr].level, sizeof(int), 1, fp);
		  (*s)->last++;
	 }
	 printf("File Loaded\n");
	 getch();
	 clrscr();
	 fclose(fp);
}

void main()
{
	 srptr s;
	 initialize(&s);

	 insert(&s);

	 write_file(s);

	 read_file(&s);


	 display(s);
}

I run the program two times. First,
It will write the data that are in function insert to file "test.dat". The read_file function call doesn't exist yet.
The second time I run the program, the function call insert and write_file are deleted and function call read_file is created.

I've been getting garbage value and I can't figure out why. Please help.

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by super-sonic
0
  1. Do not use void main().
  2. Always try to free the memory which is allocated.
  3. When you run the program for the second time by deleting insert and write_file, then the strlen function will be garbage. i.e, strlen((*s)->p, [ctr].n.FName), , strlen((*s)->p[ctr].n.LName), and strlen((*s)->p[ctr].course), in the read_file, function are garbage, hence you get garbage result when you display.

Edited by mike_2000_17: Fixed formatting

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.