Question

Create a program that filters the data in a CSV file of product data based on...

Create a program that filters the data in a CSV file of product data based on some search word and prints the resulting output to a new file. Additionally, the program will print the number of items filtered to stdout. • Your program should take three arguments: an input file to process, an output file to save the results, and a search word. • If the output file already exists or cannot be opened, warn the user by printing "CANNOT OPEN FILE." to stdout. • If a raw CSV line in the input file contains the word given by the user, that raw line must be copied to the new file AS IS. • Print the number of items found to stdout using the format string "%d item(s) returned.\n" • Save your code as prob4.c.

Example Run

$ ./a.out prices.csv filter.csv Intel

4 item(s) returned.

Homework Answers

Answer #1

Please find the requested program below. Also including the screenshot of sample output and screenshot of code to understand the indentation. Also please see the screenshot of inputfile used for testing and corresponding output file generated.

Please provide your feedback
Thanks and Happy learning!

Input file:

Ouput File created for searching the pattern 'file'

#include<stdio.h>
#include<string.h>

int ProcessFiles(const char* inputFileName, const char* outputFileName, const char* searchString)
{
    FILE * fpIn;
    FILE * fpOut;
    const int bufferLength = 255;
    char inputBuffer[bufferLength];
    int matchCount = 0;
    size_t dataLen = 0;

    //Open Input file
    if ((fpIn = fopen(inputFileName, "r")) == NULL)
    {
        printf("CANNOT OPEN INPUT FILE.\n");
        return 0;
    }

    if ((fpOut = fopen(outputFileName, "w")) == NULL)
    {
        printf("CANNOT OPEN OUTPUT FILE.\n");
        return 0;
    }

    //Read from the input file line by line
    while (fgets(inputBuffer, bufferLength, fpIn))
    {
        const char* ptr = strstr(inputBuffer, searchString);
        if (ptr != NULL)
        {
            //Found the search string in the current line
            ++matchCount;

            dataLen = strlen(inputBuffer);
            //Write the data to the output file
            if (fwrite((void*)inputBuffer, sizeof(char), dataLen, fpOut) < dataLen)
            {
                printf("Error while writing to the output file.\n");
                fclose(fpOut);
                fclose(fpIn);
                return matchCount;
            }
        }
    }

    fclose(fpOut);
    fclose(fpIn);
    return matchCount;
}

int main(int argc, char* argv[])
{
    int matchedLinesCount = 0;
    if (argc != 4)
    {
        printf("Invalid number of arguments %d. Usage: %s inputFileName outputFileName searchString\n", argc, argv[0]);
        return -1;
    }

    matchedLinesCount = ProcessFiles(argv[1], argv[2], argv[3]);
    printf("%d item(s) returned.\n", matchedLinesCount);

    return 0;
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Create a program that copies the data from one file to another while converting all lowercase...
Create a program that copies the data from one file to another while converting all lowercase vowels to uppercase vowels. Your program should accept two arguments: an input file to read from and an output file to copy to. • Your program should check to make sure the output file does not already exist. If it does, print "DESTINATION FILE EXISTS." to stdout. • Print the number of characters changed to stdout using the format string "%d characters changed." •...
C++ programming Write a program that reads a comma-separated file (CSV) with integer values and prints...
C++ programming Write a program that reads a comma-separated file (CSV) with integer values and prints the number of integer values in the file. Your program's input will be a string with the name of the file. If the file does not exist, then the program must print: Error opening the file For example, given the following CSV file input1.csv: 1,10,20,30,40 The output of your program must be: 5 You can safely assume that the input file will be a...
Writing a program in Python that reads a text file and organizes the words in the...
Writing a program in Python that reads a text file and organizes the words in the file into a list without repeating words and in all lowercase. Here is what I have #This program takes a user input file name and returns each word in a list #and how many different words are in the program. while True:   #While loop to loop program     words = 0     #list1 = ['Programmers','add','an','operating','system','and','set','of','applications','to','the','hardware',          # 'we','end','up','with','a','Personal','Digital','Assistant','that','is','quite','helpful','capable',           #'helping','us','do','many','different','things']        try:        ...
Please write a program that reads the file you specify and calculates how many times each...
Please write a program that reads the file you specify and calculates how many times each word stored in the file appears. However, ignore non-alphabetic words and convert uppercase letters to lowercase letters. For example, all's, Alls, alls are considered to be the same words. What is the output of the Python program when the input file is specified as "proverbs.txt"? That is, in your answer, include the source codes of your word counter program and its output. <proverbs.txt> All's...
IN MIPS ASSEMBLY Macro File: Create macros to print an int, print a char, print a...
IN MIPS ASSEMBLY Macro File: Create macros to print an int, print a char, print a string, get a string from the user, open file, close file, read file, and allocate heap memory. You can use more macros than these if you like. Main Program File: Allocate 1024 bytes of dynamic memory and save the pointer to the area. The main program is a loop in which you ask the user for a filename. If they enter nothing for the...
Create a Python main program which calls two functions enterNum and calcResult and accomplishes the following:...
Create a Python main program which calls two functions enterNum and calcResult and accomplishes the following: 1. The main program calls the function enterNum 3 times. The first time enterNum is called, the main program stores the returned output in the variable xx. The second time, the returned output is stored in the variable yy and the third time in zz. 2. enterNum asks the user to enter a floating point number. This function has no input arguments and returns...
Program Behavior Each time your program is run, it will prompt the user to enter the...
Program Behavior Each time your program is run, it will prompt the user to enter the name of an input file to analyze. It will then read and analyze the contents of the input file, then print the results. Here is a sample run of the program. User input is shown in red. Let's analyze some text! Enter file name: sample.txt Number of lines: 21 Number of words: 184 Number of long words: 49 Number of sentences: 14 Number of...
Create a program that generates a file of random numbers, and then prints them in neat...
Create a program that generates a file of random numbers, and then prints them in neat fashion to another file, and also saves to that file the average and standard deviation of those numbers. I) First, you would need to generate a file of random numbers that consists of N random numbers (100 < N < 1000). Each random digit should be a real number (type double) between 0 and 50. This file and its digits would now serve as...
c# please Create a “Main” program which reads a text file called “collectionOfWords.txt”. Include exception handling...
c# please Create a “Main” program which reads a text file called “collectionOfWords.txt”. Include exception handling to check if the file exists before attempting to open it. Read and print each string to the console. Next modify each word such at the first letter in each word is uppercase and all other letters are lowercase. Display the modified word on the console. Creating a text file: Open up Notepad and type in the following words. Save the file as collectionOfWords.txt...
create case 4 #include <stdio.h> int main(void) { int counter; int choice; FILE *fp; char item[100];...
create case 4 #include <stdio.h> int main(void) { int counter; int choice; FILE *fp; char item[100]; while(1) { printf("Welcome to my shopping list\n\n"); printf("Main Menu:\n"); printf("1. Add to list\n"); printf("2. Print List\n"); printf("3. Delete List\n"); printf("4. Remove an item from the List\n"); printf("5. Exit\n\n"); scanf("%i", &choice); switch(choice) { case 1: //add to list //get the input from the user printf("Enter item: "); scanf("%s", item); //open the file fp = fopen("list.txt","a"); //write to the file fprintf(fp, "\n%s", item); //close the file...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT