Thread: I need help with a few minor things.

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

    I need help with a few minor things.

    The program is meant to write the entire contents of the array to the file called Car in the c directory but it's not working for some reason I was wondering if someone can help. I think the errors are very minor but I can'nt put my finger on 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[sof]);
    
    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[sof])
    {
    	
    	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
    ---
    Join Date
    May 2004
    Posts
    1,379
    You should really check the return value of fopen() so that you know if the file was opened or not.

    Code:
    FILE *fp;
    if((fp = fopen("what.txt","w") == NULL)){
      //error
    }

    [edit]
    Ignore above post...

    You are not sending any data to the file!
    you ned to use fprintf()
    Code:
    fprintf(fp,"whatever");

Popular pages Recent additions subscribe to a feed