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." •...
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...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
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 program that sorts words of a string based on first letter of each word. Use...
Create program that sorts words of a string based on first letter of each word. Use C programming language. - Only use stdio.h and string.h - Remove any newline \n from input string - Use insert function - Input prompt should say "Enter string of your choice: " - Output should print sorted string on new line Example:     Enter string of your choice: this is a string     a is string this
This assignment involves using a binary search tree (BST) to keep track of all words in...
This assignment involves using a binary search tree (BST) to keep track of all words in a text document. It produces a cross-reference, or a concordance. This is very much like assignment 4, except that you must use a different data structure. You may use some of the code you wrote for that assignment, such as input parsing, for this one. Remember that in a binary search tree, the value to the left of the root is less than the...
** Language Used : Python ** PART 2 : Create a list of unique words This...
** Language Used : Python ** PART 2 : Create a list of unique words This part of the project involves creating a function that will manage a List of unique strings. The function is passed a string and a list as arguments. It passes a list back. The function to add a word to a List if word does not exist in the List. If the word does exist in the List, the function does nothing. Create a test...
In this assignment you will write a program that compares the relative strengths of two earthquakes,...
In this assignment you will write a program that compares the relative strengths of two earthquakes, given their magnitudes using the moment magnitude scale. Earthquakes The amount of energy released during an earthquake -- corresponding to the amount of shaking -- is measured using the "moment magnitude scale". We can compare the relative strength of two earthquakes given the magnitudes m1 and m2 using this formula: f=10^1.5(m1−m2) If m1>m2, the resulting value f tells us how many times stronger m1...
In this lab, you will write a program that creates a binary search tree based on...
In this lab, you will write a program that creates a binary search tree based on user input. Then, the user will indicate what order to print the values in. **Please write in C code** Start with the bst.h and bst.c base code provided to you. You will need to modify the source and header file to complete this lab. bst.h: #ifndef BST_H #define BST_H typedef struct BSTNode { int value; struct BSTNode* left; struct BSTNode* right; } BSTNode; BSTNode*...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT
Active Questions
  • 1. The nucleus is a membrane-bound organelle that contains the cells genetic information. What other intracellular...
    asked 8 minutes ago
  • Do genetics play a significant role in human attraction? Defend a position (yes or no). Use...
    asked 30 minutes ago
  • 9.14 The quality-control manager at a compact flu-orescent light bulb (CFL) factory needs to determine whether...
    asked 35 minutes ago
  • Describe a business you are familiar with. Discuss ONE way this firm adjusts its short-term capacity...
    asked 49 minutes ago
  • Write a persuasive speech on whether online or face to face learning/education is better for students
    asked 57 minutes ago
  • (a) As an analyst briefly explain what you will consider in applying nested designs (b) State...
    asked 1 hour ago
  • Metals are good thermal conductors — that is, when there is a temperature difference across their...
    asked 1 hour ago
  • Theoretical Perspectives that attempts to explain and answer fundamental questions why societies form and why they...
    asked 1 hour ago
  • I am working with data analysis and with a survey. I am cleaning up the following...
    asked 1 hour ago
  • For this problem, carry at least four digits after the decimal in your calculations. Answers may...
    asked 1 hour ago
  • The International Air Transport Association surveys business travelers to develop quality ratings for transatlantic gateway airports....
    asked 1 hour ago
  • Unfortunately, arsenic occurs naturally in some ground water†. A mean arsenic level of μ = 8.0...
    asked 2 hours ago