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

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...
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...
When using the import wizard in MATLAB to import data fro, a .csv file the data...
When using the import wizard in MATLAB to import data fro, a .csv file the data appears in MATLAB in the following format "35:53.2" how do I convert this into more usable matlab values? I think that the duration function was used to generate the time format. The code will need to be written in MATLAB software I will leave feedback if you are able to provide a correct response. Thank you
Write a program that computes and prints the average of numbers in a text file. I...
Write a program that computes and prints the average of numbers in a text file. I created a text file integers.txt that has the numbers 5,4,3,2,1. I need to define the average function Define the main function which will include the following things 1. prompt user for input of text file name 2. open and read input file, can be done before or inside high order functions 3. use two high order functions 4.calculate and display averages and original ist...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT