I have the following input file:
WO98EKOYMPCAUEWT0 Honda Civic 2011 4
7W32UAERZFBCB3S6P Chevrolet Tahoe 2011 6
DNU7XQO8LLA9I6YFX Toyota Tercel 2012 4
DNU7XQO8LLA9I6YFX Toyota Tercel 2012 4
DNU7XQO8LLA9I6YFX Toyota Tercel 2012 4
7W32UAERZFBCB3S6P Chevrolet Tahoe 2011 6
This is actually a task that we were handed, and i have been struggling for a few days with no results.
Read each line and put the values of the second, third and fourth in a struct.
After that add a new column which are the number of occurrences based on the 3 columns above.
Sort data by second, thrid and fourth. and then output them in a file.
Here is my code so far:
struct Car
{
char *CarMake;
char *CarModel;
char *CarMakeYear;
int Occurances;
};
int main(int argc, char *argv[])
{
//File related
FILE *inputFile, *outputFile;
char fileName[] = "";
//Struct related
int carCounter = 0;
struct Car cars[50];
struct Car car;
car.CarMake = (char*)malloc( 200 *sizeof(char));
car.CarMakeYear = (char*)malloc( 200 *sizeof(char));
car.CarModel = (char*)malloc( 200 *sizeof(char));
car.Occurances = 0;
printf("Please enter file name:", fileName);
scanf("%s",fileName);
inputFile = fopen(fileName,"r");
while(fscanf(inputFile, "%*s %s %s %s %*s\n",car.CarMake, car.CarModel,car.CarMakeYear ) != EOF)
{
cars[carCounter].CarMake = car.CarMake;
carCounter++;
}
fclose(inputFile);
int i=0;
for(i=0;i<6;i++)
printf("%s %s %s \n", cars[i].CarMake, cars[i].CarModel,cars[i].CarMakeYear);
system("PAUSE");
return 0;
}
The printf above will take the last array value and keeps printing which means it's not working. Anybody please help with this, i have till before midnight till submit.