Thread: Problem writing to file

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    92

    Problem writing to file

    I wrote this code but it does'nt write to the file all the things the user inputs. The user inputs 2 different dates and the name of the car and for how much it was purchased for but none of that show up in the file. What could be the problem with it?

    Code:
    #include <stdio.h>
    
    #define s 20
    
    #define sof 10
    
    typedef struct { 
    	int day;
    	int month;
    	int year;
    } Date;
    
    typedef struct {
    	float cost;
    } Price;
    
    typedef struct {
    	char make[s];
    	Date purchaseDate;
    	Date manufactureDate;
    	Price purchasePrice;
    } Car;
    
    int displaycar (Car car[]);
    
    int main()
    {
    	
    	Car car[sof];
    	
    	int i,j;
    	FILE *outp;
    	for(i=0; i<sof; i++)
    	{
    			printf("Please Enter the name of the car: ");
    			scanf("%s",&car[i].make);
    
                                            printf(" Please enter the day of purchase: ");
                                            scanf("%d", &car[i].purchaseDate.day);
    
                                            printf(" Please enter the month of purchase: ");
                                            scanf("%d", &car[i].purchaseDate.month);
    
                                            printf(" Please enter the year of purchase: ");
                                            scanf("%d", &car[i].purchaseDate.year);
    		
                                            printf(" Please enter the day of manufacture: ");
                                            scanf("%d", &car[i].manufactureDate.day);
    
                                            printf(" Please enter the month of manufacture: ");
                                            scanf("%d", &car[i].manufactureDate.month);
    
                                            printf(" Please enter the year of manufacture: ");
                                            scanf("%d", &car[i].manufactureDate.year);
    		
                                           printf(" Please enter the price the car was bought for: ");
                                           scanf("%f", &car[i].purchasePrice.cost);
                                           }
    outp =fopen("c:\\Car.txt", "w");
    for(j=0; j<=sof; j++)
    {
    	fprintf(outp,"%c\n", car[j]);
    }
    
    displaycar(car);
    
    return 0;
    }
    
    int displaycar (Car car[])
    
    {
    	printf(" The name of the car is %s \n", car->make);
    	
    printf(" The car was purchased on the %d of the %d of the year %d\n", (car->purchaseDate).day, (car->purchaseDate).month, (car->purchaseDate).year);
    
    printf(" The car was manufactured on the %d of the %d month of the year %d \n",(car->manufactureDate).day, (car->manufactureDate).month, (car->manufactureDate).year);
    
    printf(" The car was bought for %lf \n",(car->purchasePrice).cost);
    
    return 0;
    }

  2. #2
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    Code:
    outp =fopen("c:\\Car.txt", "w");
    its a gud programming practice to check the outp to Null.

    Code:
    for(j=0; j<=sof; j++)
    {
    	fprintf(outp,"%c\n", car[j]);
    }
    Are you expecting to write the whole info of your structure Car with this peice of code?? %c will prints only one character
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built
    for"

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    you might consider writing the entire array into a binary file instead of a text file. And make sure you close the file before returning from the function so that all the data gets flushed to the disk.

    Code:
    outp =fopen("c:\\Car.txt", "wb");
    if(outp)
    {
        fwrite(car, sof,sizeof(car[0]), outp);
        fclose(outp);
    }
    
    // read back later
    outp =fopen("c:\\Car.txt", "rb");
    if(outp)
    {
        fread(car, sof,sizeof(car[0]), outp);
        fclose(outp);
    }
    Using binary files as above makes it very easy and quick to read and write large amounts of data to the file. One problem with them is that once written the structures cannot normally be changed without writing data conversion program. Nor can you easily view the file with a text editor such as Notepad.exe.
    Last edited by Ancient Dragon; 11-03-2005 at 05:09 AM.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    for(j=0; j<=sof; j++)
    ->
    Code:
    for(j=0; j<sof; j++)
    Code:
    int displaycar (Car car[])
    Are you sure that's what you want?

    You should also close the file when you're done with it.
    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.

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    92

    Made the changes suggested

    Hello I made the changes suggested and this time instead of the program writing 1 character to file it crashed why is it taht when I used strings it crashed and why will it not put tyhe entire thing on the file.

    Code:
    outp =fopen("c:\\Car.txt", "w");
    for(j=0; j<=sof; j++)
    {
    	fprintf(outp,"%s\n", car[j]);
    }
    fclose(outp);

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    because car is a structure that contains several objects. You can't pass the entire structure to fprintf() and expect that function to decide which of the structure members to use. Garbage in -- garbage out.
    Code:
    fprintf(outp,"%s\n", car[j].make);

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    92

    I made the suggested changes but still does'nt work

    I made the changes but the output is still not what i want it to be

    car
    car
    car

    is the output but it's meant to copy the entire array into the file. Is there any way of doing it because i'am on the verge of giving up

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    what have you done so far? post most recent code. If you want to make the file readable by other text editors such as notepad.exe then you have to program it to write each member of the structure similar to the way we posted how to save the car[j].make. From this example it should not be difficult to write out the rest of the structure in similar format.

    Code:
    // save make
    fprintf(outp,"%s\n", car[j].make);
    // save purchase date in mm/dd/yyyy format
    fprintf(outp,"%02d/%02d/%04d\n", purchaseDate.month,
                  purchaseDate.day,
                  purchaseDate.year);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Creating File for File Processing
    By Dampecram in forum C Programming
    Replies: 2
    Last Post: 12-07-2008, 01:26 AM
  2. Problem with file writing
    By Goldrak in forum C++ Programming
    Replies: 7
    Last Post: 04-09-2006, 06:46 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM