Writing an array to a file

This is a discussion on Writing an array to a file within the C Programming forums, part of the General Programming Boards category; Hello I wrote this program it's meant to ask the user for a number of inputs and when the user ...

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

    Writing an array to a file

    Hello I wrote this program it's meant to ask the user for a number of inputs and when the user enters them it;a meant to save them on a file and display them on the screen as well. The problem is taht I want to wirte the contents of the entire array to the file and also to didplay it. And there is a major error in line 3 I can figure out what's wrong can somebady please help.

    Code:
    #include <stdio.h>
    
    int displaycar (Car car[]);
    
    #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 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(char_txt_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
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    You are declaring a function that takes an array of "Car", but the compiler doesn't yet know what a Car is. Put it below your typedef struct declaration of Car.

    Code:
    fprintf(char_txt_outp,"%c\n, car[j]);
    Surely your compiler provided a diagnostic about the above?

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

    I don'nt get it

    What do you mean the cmpiler does'nt know that car is and anothe problem i had was the compiler kept saying there is a syntax error in the prototype defined i just figure out what it is

  4. #4
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    I said it doesn't know what a Car is. You are declaring a function that takes an array of "Car". What is Car? It doesn't exist in C. You are declaring Car BELOW the function declaration that references Car. The compiler therefore hasn't seen it yet, that's why you get the error, simple. Put the function prototype below where you actually declare what a Car is.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Writing an array to a file.
    By Queatrix in forum C++ Programming
    Replies: 8
    Last Post: 04-24-2005, 01:41 PM
  3. writing data to a file from an array of pointers
    By Mingzhi in forum C++ Programming
    Replies: 1
    Last Post: 07-19-2004, 09:07 AM
  4. writing contents of array to file
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 06-26-2002, 04:06 PM
  5. File I/O problems!!! Help!!!
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-17-2002, 08:09 PM

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