c - Inputting values from CSV text file into arrays -
for reason program messes in first while loop don't understand why. yesterday using standard redirection input stdin , i'm assuming opening file same thing. why not working now?
this text file saved '.csv' , i'm opening:
hotdog, 10, 2, 1.50
bun, 10, 2, 0.50
burger, 100, 10, 2.00
#include <string.h> #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { int = 0, j = 0; char command[25], str[100]; int quantity[100], limit[100]; double cost[100]; char *item[100]; char *token, *ptr; file *fp = fopen("inventory.csv", "rw"); if(fp == null) { perror ("error opening file"); } //testing see if program gets until here, tries copy data csv arrays while(fgets(str, 100, fp) != null) { token = strtok (str,","); ptr = strdup(token); item[i] = ptr; sscanf (token, "%s", item[i]); token = strtok (null,","); sscanf (token, "%d", &quantity[i]); token = strtok (null,","); sscanf (token, "%d", &limit[i]); token = strtok (null,"\n"); sscanf (token, "%lf", &cost[i]); i++; } printf("hey"); for(j=0;j<i;j++) { printf("%s, %d, %d, %lf\n", item[j], quantity[j], limit[j], cost[j]); } strcpy(command, argv[1]); if(strcmp(command,"list") == 0) { for(j=0;j<i;j++) { printf("%s, %d, %d, %lf\n", item[j], quantity[j], limit[j], cost[j]); } } else { printf("wtf hello"); } return 0; }
in following statements sscanf address of entry should given
sscanf (token, "%d", &quantity[i]);
sscanf (token, "%d", &limit[i]);
sscanf (token, "%lf", &cost[i]);
Comments
Post a Comment