can any programmer.....

stan's Avatar, Join Date: Jun 2008
Newbie Member
can any programmer debug this program.
I am using this program to make a file(binary) and store some records in it.

Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
main()
{
	FILE *fp;
	char another,str[20];
	struct customer
	{
		int accno;
		char c;
		float balance;
	};
	struct customer e;

	clrscr();
	gets(str);
	fp=fopen(str,"wb+");
	if(fp==NULL)
	{
		puts("\nunable to open");
		exit(1);
	}
	another='y';
	while(another=='y')
	{
		printf("Enter accno.,name and balance:");
		scanf("%d%c%f",&e.accno,&e.c,&e.balance);
		fwrite(&e,sizeof(e),1,fp);
		printf("Add another:");
		fflush(stdin);
		another=getche();
	}
	fclose(fp);
}
and then I try to display the records using this program...
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
main()
{
	FILE *fp;
	char another,str[20];
	struct customer
	{
		int accno;
		char c;
		float balance;
	};
	struct customer e;
	
	clrscr();
	gets(str);
	fp=fopen(str,"rb+");
	if(fp==NULL)
	{
		puts("\nunable");
		exit(1);
	}
	while(fread(&e,sizeof(e),1,fp)==1)
		printf("\n%d %c %f",e.accno,e.c,e.balance);
	fclose (fp);
}
but it displays e.accno and then displays some rubbish negative value for e.c and e.balance.
Plz help me if you can.

Last edited by shabbir; 28Jun2008 at 11:47.. Reason: Code block
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Try displaying e.accno, e.c and e.balance after you've read them in with scanf.