Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   can any programmer..... (http://www.go4expert.com/forums/can-any-programmer-t11687/)

stan 27Jun2008 22:34

can any programmer.....
 
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.

xpi0t0s 16Jul2008 16:09

Re: can any programmer.....
 
Try displaying e.accno, e.c and e.balance after you've read them in with scanf.


All times are GMT +5.5. The time now is 14:48.