C Programming Problem 2 Car Listings with 10 points extra cr
C Programming
Problem 2. Car Listings (with 10 points extra credit) Write a program the processes a file with car sale ads and filters them based on make and model. The filtered entries are saved to a new file. A car ad file has 0 or more lines with this format: make, model, miles, year, price make and model are strings with maximum 20 characters including the 10), while miles, year, and price occupy maximum 10 characters (including the \'10). The content for an example car ad file isSolution
#include<stdio.h>
#include<string.h>
int filterCars(const char adFileName[],const char make[], const char model[], const char outFileName[]);
int main(int argc, char *argv[]){
filterCars(\"cars.txt\",\"Honda\",\"Civic\",\"output.txt\");
}
int filterCars(const char adFileName[],const char make[], const char model[], const char outFileName[]){
int i;
FILE *ptr_read_file;
char rbuf[1000];
FILE *ptr_write_file;
char wbuf[1000];
ptr_write_file =fopen(outFileName, \"w\");
ptr_read_file =fopen(adFileName,\"r\");
if (!ptr_read_file)
return 1;
if (!ptr_write_file)
return 1;
while (fgets(rbuf,1000, ptr_read_file)!=NULL){
//printf(\"%s\",rbuf);
char * pch;
pch = strtok (rbuf,\", \");
while (pch != NULL){
//printf (\"%s\ \",pch);
pch = strtok (NULL, \", \");
}
if(strcmp(pch[0],make) && strcmp(pch[1],model)){
fputs(rbuf,ptr_write_file);
}
printf(\"\ \");
}
fclose(ptr_write_file);
fclose(ptr_read_file);
printf(\"\ \");
return 0;
}
