fread/fwrite problems

This is a discussion on fread/fwrite problems within the C Programming forums, part of the General Programming Boards category; Greetings master coders. Earlier on I was having trouble with some fwrite code. I've progressed a little from there, but ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    5

    fread/fwrite problems

    Greetings master coders.

    Earlier on I was having trouble with some fwrite code. I've progressed a little from there, but now I'm stuck again.

    In the code below, I'm trying to write data to a binary file, and then read the file and display the data again.

    Code:
    #include <stdio.h>
    #include <math.h>
    #define MAXLEN 20
    #define FLEETMAX 5
    
    /*Define general struct for dates*/
    typedef struct
    {
    	int day;
    	int month;
    	int year;
    }Dates;
    
    /*Define struct for cars*/
    typedef struct
    {
    	char carmake[MAXLEN];
    	Dates manufactureDate;
    	Dates purchaseDate;
    	double purchasePrice;
    }Cars;
    
    int writeBinary(Cars *car);
    int readBinary(Cars *car);
    
    int main()
    {
    	int i, j, menu;
    	Cars fleet[FLEETMAX];
    	Cars temp[FLEETMAX];
    	i = 0;
    	while(menu != 6){
    		scanf("%d", &menu);
    		
    		switch(menu)
    		{
    			case 1:
    				addCar(&fleet[i]); /*Code which gets input from user via scanf*/
    				i++;
    				break;
    					
    				
    			/*...Some other cases...*/
    
    			case 4: /*Save entered cars into a binary file*/
    				printf("\nSaving to file...\n");
    				for(j=0; j<i; j++)
    				{
    					writeBinary(&fleet[j]);
    				}
    				break;
    
    			case 5: /*Load fleet from existing binary file*/
    				printf("\nReading saved file...\n");
    
    				for(j=0; j<i; j++)
    				{
    					printf("Read loop count: %d\n", j);
    					readBinary(&temp[j]);
    				}
    				break;
    		}
    }
    
    	/*Function to write all cars in current fleet to binary file*/
    int writeBinary(Cars *car)
    {
    	int i;
    	FILE *binaryp;
    
    	binaryp = fopen("test.bin", "wb"); /*Open binary file for read/write*/
    	if (binaryp == NULL)
    	{
    		printf("Error. Could not open file.\n");
    		return (1);
    	}
    
    	fwrite(&car, sizeof(Cars), 1, binaryp);
    	fclose(binaryp);
    
    	printf("Saved.\n");
    	return (0);
    }
    
    /*Function to read fleet from binary file and load into program*/
    int readBinary(Cars *car)
    {
    	FILE *binaryp;
    	
    	int i;
    
    	binaryp = fopen("test.bin", "rb"); /*Open binary file for reading*/
    	if (binaryp == NULL)
    	{
    		printf("Error. Could not open file.\n");
    		return (1);
    	}
    
    	fread(&car, sizeof(Cars), 1, binaryp);
    	printf("Carmake: %s\n", (*car).carmake);
    	printf("Purchase price: $%.2f\n\n", (*car).purchasePrice);
    	
    	fclose(binaryp);
    	printf("Read load finished.\n");
    	 return (0);
    }
    The problem I get is the output is not what it's supposed to be.
    For example, when 2 inputs have been saved, the output should read:

    Code:
    Carmake: Ford
    Purchase price: $111.00
    
    Carmake: Nissan
    Purchase price: $222.00
    Instead however, the actual output is:
    Code:
    Carmake: Nissan
    Purchase price: $222.00
    
    Carmake: Nissan
    Purchase price: $222.00
    The code seems to only be copying/displaying the last (i.e. second) entered output TWICE and there's no sign of the first input at all. I'm not sure if it's my logic that's wrong, or if I'm incorrectly using my fread and fwrite, or if it's some other problem. I'd greatly appreciate your help as I'm completely stuck.

    Many thanks!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Your writebinary function is called several times in a loop. The first instruction in your function is "completely destroy the previous contents of the file". Naturally you end up with no other cars in your file.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    5
    Wow. I don't believe I missed that! Thanks very much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 04-27-2009, 06:56 PM
  2. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 03:59 AM
  3. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  4. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 04:35 PM

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