Hello,
I've been trying to read and print the contents of a text file. The text file is like this:
1234 Orange Toyota 2010
5487 Blue Audi 1985
This is my code. The compiler shows NO erros but the program doesn't run.
Code:
#include <stdio.h>
#include <stdlib.h>
#define CARS_FILE "cars.txt"
typedef struct {
int plateNumber, year;
char color[10];
char brand[10];
} Car;
int main()
{
Car *cars;
int i=0;
FILE *myFile;
myFile = fopen(CARS_FILE, "r");
if(myFile == NULL)
{
printf("ERROR!\n");
return 1;
}
while(fscanf(myFile,"%d\t%s\t%s\t%d\n", &cars[i].plateNumber, &cars[i].color, &cars[i].brand, &cars[i].year)!= EOF)
{
printf("%d\t%s\t%s\t%d\n", cars[i].plateNumber, cars[i].color, cars[i].brand, cars[i].year);
}
fclose(myFile);
system("PAUSE");
return 0;
}
Any help would be much appreciated...