Reading a csv file using C and then doing string manipulation -
first open csv file (cars.csv) using c file cars.c. csv file like:
toyota, 100, 20, 150.50 nissan, 200, 50, 100.75 bmw, 400, 80, 323.00 the 1st column names, 2nd column stock, 3rd column order, 4th column price.
then have 2 works(i call program in commandline work name , other argument need pass):
list items in csv file while commas cannot part of output. have print headers each column. (./cars list)
i send argument when running program command line. argument 1 name of items in csv file. have subtract 1 stock , update csvfile. (./cars reduce nissan)
this how opened csv file.
void main(int argc, char *argv[]) { file *in = fopen ("cars.csv" ,"rt"); if (in == null) { fclose (in); return 0; } //i need store data csv file in array of pointers. know how that, im having problem in storing them without commas , doing need in part 1.
//this code write 2nd part.
int p =0; for(p =0; p<; p = p + 4) { if(strcmp(argsv[2],save[p]) == 0) { //save array of pointers int r = int(save[p +1]); //store data csv file int s = r - 1; char str[15]; sprintf(str, "%d", s); save[p +1] = str; // not sure if line makes sense } }
there couple of possible solutions.
here's one:
read lines array of pointers. allocate array
malloc, reallocate neededrealloc. read linesfgets.for each line, use
strtoksplit line, , whatever need each field.
another solution:
use favorite search engine search existing csv-reading library (there many out there, can handle advanced csv files).
Comments
Post a Comment