Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C program that processes a file with car sale ads and filters them based

ID: 3572698 • Letter: W

Question

Write a C program that processes a file with car sale ads and filters them based on make and model. The filtered entries are saved to a new file. We can say that this program implements searching through a car ad database based on make and model.

A car ad file has 0 or more lines with this format:

make, model, miles, year, price

make and model are strings with maximum each of 20 characters (including the ‘’), while miles, year, and price occupy each a maximum of 10 characters (including the ‘’).

For example, the content for an sample car ad file could be:

Honda, Civic, 122210, 2004, 5900

Volvo, V70, 43900, 2010, 21000

Lada, 1500, 130000, 1983, 32000

The function uses the prototype :

int filterCars(const char adFileName[], const char make[], const char model[], const char outFileName[]);

the function must open the input file with the name given in parameter adFileName in readonly mode and must open the output file with the name given in parameter outFileName in write mode. After that, function filterCars() reads in a loop lines from the input file. For each line read, the function checks if the make and model from the file match the make and model string parameters (ignore model if the model string is equal to “ignore”) using the strcmp() function. If true, the entire line will be written to the file with name given in string parameter outFileName.

You should make a copy of the line read before using function strtok() on it. If there is no match the function goes on to the next line. And so on until EOF.

the main function()

reads from the terminal the input file name

reads from the terminal the output file name

reads from the terminal the make string

reads from the terminal the model string (user could type “ignore” in order to not check the model)

calls the filterCars() function with the corresponding parameters.

reports (prints) how many lines were saved to the output file.

Note: there is no loop in main().

The input file name is “ads.txt” (with the content given in the example above), the output file name is “out.txt”, and the user enters make “Honda” and model “Civic”, then the function will write to file named “out.txt” the following content:

The function should return 2.

If the user enters the same data, with model string “ignore”, the function would write to file “out.txt” the following content:

Honda, Civic, 122210, 2004, 5900

Honda, Accord, 21000, 2015, 19000

Honda, Civic, 9000, 2016, 17000

Explanation / Answer

#include<stdio.h>

#include<string.h>


// Function prototype declaration
int filterCars(const char adFileName[], const char make[], const char model[], const char outFileName[]);

int filterCars(const char adFileName[], const char make[], const char model[], const char outFileName[])
{
   char name[5][20]; // To hold model, make, year, miles etc
   FILE *ifp, *ofp;
   int i;
   int linecount = 0; //number of lines pushed onto output file
   char line[80]; // string to store a line from adFileName
   char tmp[80]; // temporary line holder
   char *key;

   ifp = fopen(adFileName,"r"); //opening input file in readonly mode
   if(ifp == NULL)
   {
       return (-1);
   }

   ofp = fopen(outFileName, "a+");
   if(ofp == NULL) //could not be able to open the output file
   {
       return (-1);
   }

   i = 0;
   while( fgets(line, 80, ifp) != NULL)
   {
       i=0;
       strcpy (tmp, line);

       key = strtok(line,",");
       while(key != NULL)
       {

           strcpy(name[i], key);
           key= strtok(NULL,",");
           i++;
       }
//       printf(" %s %s ", name[0], make);
       if ( ( (strcmp (name[0], make) == 0 )
           && ( strcmp(name[1], model) == 0)
       )
       ||
       ( (strcmp (name[0], make ) == 0 )
           && ( strncmp(model, "ignore", 6) == 0)
       )
       )
       {

           fputs(tmp, ofp);
           linecount++;

       }
   }


fclose(ifp);
fclose(ofp);
return (linecount); // Returns numbber lines written to output file
}

/* main function */
int main(int argcount, char *argv[])
{

   int count;
   if (argcount != 5 ) { /* Tests correct number of inputs given to program */
       printf(" you entered arguments(%d) ", argcount);
       printf("please enter input file name, make , model, and output filename ");
       getch();
       return 1;
   }

count = filterCars(argv[1],argv[2], argv[3],argv[4]);
if(count == -1)
{
   printf("Something went wrong ");
}

printf("Number of lines (%d) written to %s ",count, argv[4]);

getch();
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote