c - Trying to rewrite a file by copying into a new one after making changes and then renaming to the old filename? -


to run program write:
inventory deduct itemname

which deducts 1 quantity of whichever fooditem name.

my input comma separated value text file (.csv). here text file saved (.csv):

hotdog, 10, 2, 1.50   bun, 10, 2, 0.50   burger, 100, 10, 2.00   

scanning arrays works. i'm little confused how should rewriting file. tried using putc i'm getting error fputs(item[j],fp2);

74  6   c:\coding\inventory.c   [warning] passing argument 1 of 'putc' makes integer pointer without cast [enabled default] 

ok, no more compiling errors. replica.csv file that's being created weird. i'm trying same format .csv above. clue i'm going wrong? also, rename isn't changing "replica.csv" "inventory.csv". file still called "replica.csv"

thanks.

#include <string.h> #include <stdio.h> #include <stdlib.h>  int main(int argc, char *argv[]) {     int = 0, j = 0;     char command[50], argument[50], str[100];     int quantity[100], limit[100];     double cost[100];     char *item[100];     char *token, *ptr;      file *fp1 = fopen("inventory.csv", "r");     if(fp1 == null)     {         perror ("error opening file");     }      while(fgets(str, 100, fp1) != 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++;     }      strcpy(command, argv[1]);              if(strcmp(command,"deduct") == 0)             {                 strcpy(argument, argv[2]);                  for(j=0;j<i;j++)                 {                     if(strcmp(argument,item[j]) == 0)                     quantity[j]--;                  }                 file *fp2 = fopen("replica.csv", "w");                 for(j=0;j<i;j++)             {                 fprintf (fp2, "%s,%d,%d,%.2lf\n", item[j], quantity[j], limit[j], cost[j]) ;             }                 fclose(fp1);                 fclose(fp2);                 remove("inventory.csv");                 rename("replica.csv", "inventory.csv");             }     return 0; } 

the second for(j=0;j<i;j++) loop should this:

file *fp2 = fopen("replica.csv", "w");  for(j=0;j<i;j++) {   fprintf (fp2, "%s,%d,%d,%lf\n", item[j], quantity[j], limit[j], cost[j]) ; } 

you using fputs types other char*, program won't compile. , using putc int treats argument char, e.g. putc(65, fp) a , not 65 in fp file.

there other problems in code.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -