Question

In: Computer Science

Create a program that parses a CSV file of product data and prints the items with...

Create a program that parses a CSV file of product data and prints the items with a price that is less than or equal to that input by the user. • Your program should take two arguments: an input file to process and a price limit. • Print only the names of each item to stdout that have a price less than or equal to the given limit. • If the given file does not exist or cannot be opened, warn the user by printing "CANNOT OPEN FILE." to stdout. • Save your code as prob3.c.

Example Run

$ ./a.out prices.csv 199.99

MSI X470 Gaming Plus

Corsair DDR4 Memory

Solutions

Expert Solution

Program

#include <stdio.h>

#include<stdlib.h>
int main(int argc, char *argv[])
{

   FILE *fp;
   char name[100],value[100],c;
   int flag,i=0,j=0;
   float v,amt;
   if(argc==3)
   {
       fp = fopen(argv[1], "r");
       if(fp == NULL)
       {
           fprintf(stderr, " can't open %s\n", argv[1]);

       }
       else
       {
           flag=1;
           while((c = getc(fp)) != EOF )
           {
               if(c!=',' && flag==1)
               {
                   flag=1;
                   name[i++]=c;
               }
               else if(c=='\n')
               {

                   flag=1;
                   name[i]='\0';
                   value[j]='\0';
                   i=0;
                   j=0;
                   v=atof(value);
                   amt=atof(argv[2]);
                   if(v<=amt)
                       printf("%s\n",name);
               }
               else
               {
                   flag=0;
                   if(c!=',')
                   value[j++]=c;
               }
           }

           name[i]='\0';
           value[j]='\0';
           v=atof(value);
           amt=atof(argv[2]);
           if(v<=amt)
               printf("%s\n",name);
           fclose(fp);
       }
   }

   return 0;
}

Output

product.csv

samsung tv,2500.00
lllg tv,32000.50
onida tv,150000.0
acer laptop,5600.0
dell lap,18000.80

$ ./a.out product.csv 25000.00

samsung tv

acer laptop

dell lap


Related Solutions

Write a program that reads and parses the CSV file, and can sort and filter data...
Write a program that reads and parses the CSV file, and can sort and filter data according to the user input and print the results. Your program should be able to do the following according to the user input: 1. Show results from a specific country 2. Sort the data according to any column 3. Filter the results (for example, >10000 cases) 4. Print the top or bottom n results You get bonus points if your program • Can do...
I am trying to create a program that reads from a csv file and finds the...
I am trying to create a program that reads from a csv file and finds the sum of total volume in liters of liquor sold from the csv file by county and print that list out by county in descending order. Currently my program runs and gives me the right answers but it is not in descending order. I tried this:     for county, volume in sorted(sums_by_volume.items(), key=lambda x: x[1], reverse=True):         index +=1         print("{}. {} {:.2f}".format(county, sums_by_volume[county]))      When I run...
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...
In this programming assignment, you will write a program that reads in the CSV file (passenger-data-short.csv),...
In this programming assignment, you will write a program that reads in the CSV file (passenger-data-short.csv), which contains passenger counts for February 2019 on 200 international flights. The data set (attached below) is a modified CSV file on all International flight departing from US Airports between January and June 2019 reported by the US Department of Transportation. You write a program that give some summary statistics on this data set. Create a header file named flights.h. In this file, you...
build a python program that will be performing: - Read a CSV file 'annual.csv' enterprise into...
build a python program that will be performing: - Read a CSV file 'annual.csv' enterprise into a data structure - Count the number of rows and columns - Determine if the data contains empty values - Replace the empty values by 'NA' for strings, '0' for decimals and '0.0' for floats - Transform all Upper case characters to Lower case characters - Transform all Lower case characters to Upper case characters - save back the 'repaired' array as csv -...
Create a program that creates a sorted list from a data file. The program will prompt...
Create a program that creates a sorted list from a data file. The program will prompt the user for the name of the data file. Create a class object called group that contains a First Name, Last Name, and Age. Your main() function should declare an array of up to 20 group objects, and load each line from the input file into an object in the array. The group class should have the following private data elements: first name ,last...
c++ Create a program that creates a sorted list from a data file. The program will...
c++ Create a program that creates a sorted list from a data file. The program will prompt the user for the name of the data file. Create a class object called group that contains a First Name, Last Name, and Age. Your main() function should declare an array of up to 20 group objects, and load each line from the input file into an object in the array. The group class should have the following private data elements: first name...
Create a program that will loop and prompt to enter the highlighted data items in the...
Create a program that will loop and prompt to enter the highlighted data items in the structure below. This is every item except customerNumber , isDeleted and newLine; const int NAME_SIZE = 20; const int STREET_SIZE = 30; const int CITY_SIZE = 20; const int STATE_CODE_SIZE = 3; struct Customers { long customerNumber; char name[NAME_SIZE]; char streetAddress_1[STREET_SIZE]; char streetAddress_2[STREET_SIZE]; char city[CITY_SIZE]; char state[STATE_CODE_SIZE]; int zipCode;     char isDeleted;     char newLine; }; Always set the item isDeleted to 'N' and...
Create a graphical spell checker program that The File menu has the three items shown. On...
Create a graphical spell checker program that The File menu has the three items shown. On Open, the program reads a text file (!) with the current directory set to the directory where your program is located. Save saves the current text in the window to the file selected over a dialog and The Edit menu has one menu item: Spell Check.
Using the data from the csv file, answer the questions with rstudio # number_children - The...
Using the data from the csv file, answer the questions with rstudio # number_children - The number of children in the home # internet - Does the home have internet access? # mode - The way the household took the survey # own - Do the residents own with or without a mortgage or rent? # language - The primary language spoken in the home # decade_built - The decade the home was built 1) In how many households, wife’s...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT