C
Chathu
Hello everyone...........
I have a problem on retriving a content of a binary file I wrote into.
My program user structures, dynamic allocation of memory and files. I
take the infomation into a dynamicaly allocated structure and then
write it to a binary file using fwrite() with appending mode. I can
retrive the data I wrote while the program is running. But if I stop
the program and re-execute it I can't read the things I wrote to the
file last time(only some vieard characters appear), but the the data I
wrote this time appeard without any problem.
How can I retrive the data I wrote to the file previousy.
Souce code of the program I wrote is as follows,
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<string.h>
#include<ctype.h>
typedef struct student{
char *name;
int age;
}stu;
void stu_initializer(stu *p,FILE *fp);
void print_stu(stu *p, FILE *fp);
void main()
{
clrscr();
stu *p;
FILE *fp;
//allocate memory for the structure
p=(stu*)malloc(sizeof(stu));
fp=fopen("stu.dat","a+b");
stu_initializer(p,fp);
fclose(fp);
clrscr();
fp=fopen("stu.dat","rb");
print_stu(p,fp);
fclose(fp);
free(p);
}////end main
/////////////////////////////////////////////////////////////////////
//set the information about the parties
void stu_initializer(stu *p,FILE *fp)
{
int a;
char buffer[81];
do{clrscr();
printf("Enter the student name: ");
gets(buffer); fflush(stdin);
//alocate memory for the stu name inside the structure
p[0].name=(char*)malloc((strlen(buffer)+1)*sizeof(char));
strcpy(p[0].name,buffer);//copy the name to the structure
//age of the student
printf("\nEnter the index of the stu? ");
scanf("%d",&p[0].age);
fflush(stdin);
//write to the file
fwrite(p,sizeof(*p),1,fp);
printf("\n\nAny more students?[Y][N] ");
}while(toupper(getch())=='Y');
clrscr();
fflush(stdin);//clear the input buffer
}//end function
/////////////////////////////////////////////////////////////////////
//print the stu list
void print_stu(stu *p, FILE *fp)
{
printf("%s %s \n","Name","Age");
while((fread(p,sizeof(*p),1,fp))==1)
printf("%s %d\n",p[0].name,p[0].age);
}
Please Help me on this problem.......
I have a problem on retriving a content of a binary file I wrote into.
My program user structures, dynamic allocation of memory and files. I
take the infomation into a dynamicaly allocated structure and then
write it to a binary file using fwrite() with appending mode. I can
retrive the data I wrote while the program is running. But if I stop
the program and re-execute it I can't read the things I wrote to the
file last time(only some vieard characters appear), but the the data I
wrote this time appeard without any problem.
How can I retrive the data I wrote to the file previousy.
Souce code of the program I wrote is as follows,
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<string.h>
#include<ctype.h>
typedef struct student{
char *name;
int age;
}stu;
void stu_initializer(stu *p,FILE *fp);
void print_stu(stu *p, FILE *fp);
void main()
{
clrscr();
stu *p;
FILE *fp;
//allocate memory for the structure
p=(stu*)malloc(sizeof(stu));
fp=fopen("stu.dat","a+b");
stu_initializer(p,fp);
fclose(fp);
clrscr();
fp=fopen("stu.dat","rb");
print_stu(p,fp);
fclose(fp);
free(p);
}////end main
/////////////////////////////////////////////////////////////////////
//set the information about the parties
void stu_initializer(stu *p,FILE *fp)
{
int a;
char buffer[81];
do{clrscr();
printf("Enter the student name: ");
gets(buffer); fflush(stdin);
//alocate memory for the stu name inside the structure
p[0].name=(char*)malloc((strlen(buffer)+1)*sizeof(char));
strcpy(p[0].name,buffer);//copy the name to the structure
//age of the student
printf("\nEnter the index of the stu? ");
scanf("%d",&p[0].age);
fflush(stdin);
//write to the file
fwrite(p,sizeof(*p),1,fp);
printf("\n\nAny more students?[Y][N] ");
}while(toupper(getch())=='Y');
clrscr();
fflush(stdin);//clear the input buffer
}//end function
/////////////////////////////////////////////////////////////////////
//print the stu list
void print_stu(stu *p, FILE *fp)
{
printf("%s %s \n","Name","Age");
while((fread(p,sizeof(*p),1,fp))==1)
printf("%s %d\n",p[0].name,p[0].age);
}
Please Help me on this problem.......