Data read off disk problem

This is a discussion on Data read off disk problem within the C Programming forums, part of the General Programming Boards category; I have a problem. This code reads accurately the account names off disk, but not the balance. It just shows ...

  1. #1
    Sly
    Sly is offline
    Registered User
    Join Date
    Nov 2008
    Location
    My computer
    Posts
    65

    Data read off disk problem

    I have a problem. This code reads accurately the account names off disk, but not the balance. It just shows up as $0.00. Please tell me what the problem is.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    void addAccount(void);
    void listAccounts(void);
    
    struct account_data
    {
           char name[60];
           double balance;
           struct entry
           {
                  int check_num;
                  char info[128];
                  union
                  {
                       double debit;
                       double credit;
                  };
           };
    };
    
    int main()
    {
    	char c;
    	int done = 0;
    	char go;
    	while(done == 0)
    	{
    		puts("\nDigital Checkbook\n");
    		puts("A - Add new account\n");
    		puts("L - List accounts\n");
    		puts("Q - Quit\n");
    		printf("Your choice: ");
    		
    		c = getchar();
    		fflush(stdin);
    		c = toupper(c);
    		switch(c)
    		{
    			case ('A'):
    				addAccount();
    				break;
    			case ('L'):
    				listAccounts();
    				break;
    			case ('Q'):
    				printf("\nAre you sure you want to quit? Y/N: ");
    				go = getchar();
    				go = toupper(go);
    				if(go == 'Y')
    				{
    					done = 1;
    					fflush(stdin);
    					system("cls");
    				}
    				else if(go == 'N')
    				{
    					fflush(stdin);
    					system("cls");
    				}
    				break;
    			default:
    				puts("\nInvalid answer ");
    		} /*End switch*/
    	} /*End while*/
    	return(0);
    } /*End main()*/
    
    void addAccount(void)
    {
    	FILE *Accounts;
    	struct account_data account;
    	
    	printf("Enter account name: ");
    	scanf("%s",&account.name);
    	printf("How much money is currently in it? $");
    	scanf("%f",&account.balance);
    	
    	Accounts = fopen("account.dat","a");
    	if (Accounts == NULL)
    	{
                     puts("Error opening file");
                     exit(1);
        }             
        
        fwrite(&account,sizeof(account),1,Accounts);
        
        fclose(Accounts);
        puts("Account added!");
        fflush(stdin);
    }
    
    void listAccounts(void)
    {
    	FILE *Accounts;
    	struct account_data account;
    	int x;
    	Accounts = fopen("account.dat","r");
    	if(Accounts == NULL)
    	{
    		puts("No data in file, or file not found.");
    		return;
    	}
    	
    	while(1 == 1)
    	{
    		x = fread(&account,sizeof(account),1,Accounts);
    		
    		if(x == 0) break;
    		
    		printf("\nAccount name:%s\n",account.name);
    		printf("Balance: $%.2f\n",account.balance);
    		
    	}
    	fclose(Accounts);
    }
    That is the relevant part.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The problem is that it is not possible to read into a double variable with %f. Hence you won't get a valid result later when you try to write it or read it.

    You can read into a double variable with %lf, though.

  3. #3
    Sly
    Sly is offline
    Registered User
    Join Date
    Nov 2008
    Location
    My computer
    Posts
    65
    Thank you. And in Line 21, I get "warning: declaration does not declare anything". Huh?
    Last edited by Sly; 01-13-2009 at 02:51 PM.

  4. #4
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Code:
     Accounts = fopen("account.dat","r");
    should be

    Code:
     Accounts = fopen("account.dat","r+b");
    and you shoudl change
    Code:
    	Accounts = fopen("account.dat","a");
    as well
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Which line is line 21?

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    And in Line 21, I get "warning: declaration does not declare anything". Huh?
    Code:
    struct entry
           {
                  int check_num;
                  char info[128];
                  union
                  {
                       double debit;
                       double credit;
                  };
           };
    You probably meant
    Code:
    struct { /* ... */ } entry;
    which would actually declare a structure called entry inside of every account_data.

    Also: cpwiki.sf.net/fflush
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Sly
    Sly is offline
    Registered User
    Join Date
    Nov 2008
    Location
    My computer
    Posts
    65
    Thanks, but when there is a comma inserted in the monetary values, it does not interpret it correctly. Why?

  8. #8
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,249
    Quote Originally Posted by Sly View Post
    Thanks, but when there is a comma inserted in the monetary values, it does not interpret it correctly. Why?
    Why would it? Commas aren't part of valid numbers (unless you're in a locale where comma is the decimal separator)
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  9. #9
    Sly
    Sly is offline
    Registered User
    Join Date
    Nov 2008
    Location
    My computer
    Posts
    65
    How do I make it accept it?

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    You could write your own number-parsing code. Or you could strip all commas before you give sscanf() the string to convert to a number. (Which wouldn't be very robust.)

    Or you could just say, "Enter the balance in dollars without commas"!

    If you really want to allow commas, I'd write your own number parser. That way you could allow an optional '$' sign at the beginning of a number, ensure commas only occur at thousands separators, generate an error if more than two digits occur after the decimal point, etc.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read size of data array when reading .txt data
    By Taquito in forum C Programming
    Replies: 13
    Last Post: 04-29-2007, 01:52 AM
  2. read data from file
    By matth in forum C++ Programming
    Replies: 3
    Last Post: 04-21-2005, 09:37 AM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Client-Server Data Read Problem
    By bob2509 in forum C Programming
    Replies: 8
    Last Post: 11-06-2002, 10:47 AM
  5. data read problem
    By Supra in forum C Programming
    Replies: 0
    Last Post: 02-03-2002, 06:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21