In: Computer Science
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
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