Thread: Examples showing fwrite and fread give back garbage data

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    182

    Examples showing fwrite and fread give back garbage data

    This is my first venture into file streams

    I've looked over the source-code carefully and couldn't find any differences from the book. Maybe it has something to do with my system?

    Here is the console output of my program
    Code:
    -1208617819               DZ۱汩6␤     -0.05
    -1079416719ƒ␤␤┌┌└F└V└└└└F└V└└└     -0.05
    -1079415338└└┼+┼>┼U┼]┼⎺┼┼>┼U┼]┼⎺┼┼     -0.05
    -1079415137┼=⎺]⎺┘⎺┬⎺™⎺⎺⎺⎺┬⎺™⎺⎺⎺⎺     -0.05
    100                                   0.00
    1000                           598008216632976371697735644702348584536405246902083247220875921706093794716375762404103004004990504111786204800430166155026589905240121409306972824498630058082565445957748647537262486126146850127872.00
    3553334                                 0.00
    ␊┤␊┼␊@␊┤␊┼␊-┌▒⎻├⎺⎻:/␌␋┌␊⎽$
    That last line is actually my console. All alphabetic lower-case characters are screwed up afterwards.




    Here is what is used to write to the file
    Code:
    #include <stdio.h>
    
    struct clientData {
    	int acctNum;
    	char lastName[ 15 ];
    	char firstName[ 10 ];
    	double balance;
    };
    
    
    int main()
    {
    	FILE *cfPtr;
    	struct clientData client = { 0, "", "", 0.0 };
    
    	if ( ( cfPtr = fopen( "credit.dat", "r+" ) ) == NULL )
    		printf( "File could not be opened.\n" );
    	else {
    		printf( "Enter account number "
    			   " ( 1 to 100, 0 to end input )\n? " );
    		scanf( "%d", &client.acctNum );
    
    		while ( client.acctNum != 0 ) {
    			printf( "Enter lastname, firstname, balance\n? " );
    			fscanf( stdin, "%s%s%lf", client.lastName, client.firstName, &client.balance );
    			fseek( cfPtr, ( client.acctNum - 1 ) * sizeof( struct clientData ), SEEK_SET );
    			fwrite( &client, sizeof( struct clientData ), 1, cfPtr );
    			printf( "Enter account number\n? " );
    			scanf( "%d", &client.acctNum );
    		}
    
    		fclose( cfPtr );
    	}
    
    	return 0;
    }



    Here is the example to read from the file, the 1st block I posted is this programs output.
    Code:
    #include <stdio.h>
    
    struct clientData {
    	int acctNum;
    	char lastName[ 15 ];
    	char firstName[ 10 ];
    	double balance;
    };
    
    
    int main()
    {
    	FILE *cfPtr;
    	struct clientData client = { 0, "", "", 0.0 };
    
    	if ( ( cfPtr = fopen( "credit.dat", "r" ) ) == NULL )
    		printf( "File could not be opened.\n" );
    	else {
    		printf( "%-6s%-16s%-11s%10s\n", "Acct", "Last Name", "First Name", "Balance" );
    
    		while ( !feof( cfPtr ) ) {
    			fread( &client, sizeof( struct clientData ), 1, cfPtr );
    
    			if ( client.acctNum != 0 )
    				printf( "%-6d%-16s%-11s%10.2f\n", 
    					  client.acctNum, client.lastName, 
    					  client.firstName, client.balance );
    		}
    
    		fclose( cfPtr );
    	}
    
    	return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The only things I spotted:
    1. I changed "r+" to "a+", otherwise the first program wouldn't create a new file.
    2. Because you are using feof incorrectly in the second file, the last record gets printed twice.

    But it prints words in the records et cetera just fine.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    182
    Interesting

    The a+ must have fixed it after I deleted the old "credit.dat". Maybe I got some garbage in the file by trying to view it with the cat command after I created it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Image Processing Help Required
    By dantu1985 in forum C Programming
    Replies: 7
    Last Post: 12-31-2007, 09:30 AM
  2. Weird problem with fwrite() and fread()
    By piote in forum C Programming
    Replies: 2
    Last Post: 11-13-2004, 03:07 PM
  3. buffer type for fread & fwrite
    By daluu in forum C Programming
    Replies: 5
    Last Post: 05-08-2003, 06:57 PM
  4. fread() and fwrite() ?
    By Limblet in forum C Programming
    Replies: 4
    Last Post: 09-25-2001, 07:36 PM