can any programmer.....

Discussion in 'C' started by stan, Jun 27, 2008.

  1. stan

    stan New Member

    Joined:
    Jun 27, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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 a moderator: Jun 28, 2008
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Try displaying e.accno, e.c and e.balance after you've read them in with scanf.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice