-1

So I'm writing a program that takes data from a CSV txt file to an array structure. This data will be used to manage an inventory. I had my entire program working, but all of a sudden it crashes every time I run. Iv'e got the crash narrowed down to my file reading function, and was wondering if anyone could see the problem. Here is the initial file data.

1000,1.49,3.79,10,0,Fish Food
2000,0.29,1.59,100,1,Angelfish
2001,0.09,0.79,200,0,Guppy
5000,2.40,5.95,10,0,Dog Collar (Large)
6000,49.99,129.99,3,1,Dalmatian Puppy

Here is the structure deceleration

struct inventory_s
{
 int productNumber;
 float mfrPrice;
 float retailPrice;
 int numInStock;
 char liveInv;
 char productName[PRODUCTNAME_SZ];
};

The structure array

struct inventory_s inventory[MAX_INVENTORY];

Here is the code that I have

    FILE* pFile;
char *buf = malloc(MAX_INVENTORY);
char *info;
if ( ( pFile = fopen( "inventory.txt", "r" ) ) == NULL ) //Reading a file
{
    printf( "File could not be opened.\n" );
}

int i = 0;
while (fgets(buf, MAX_INVENTORY, pFile) != NULL)
{
    if ((strlen(buf)>0) && (buf[strlen (buf) - 1] == '\n'))
        buf[strlen (buf) - 1] = '\0';

    info = strtok(buf, ",");
    inventory[i].productNumber = atoi(info);

    info = strtok(NULL, ",");
    inventory[i].mfrPrice = atof(info);

    info = strtok(NULL, ",");
    inventory[i].retailPrice = atof(info);

    info = strtok(NULL, ",");
    inventory[i].numInStock = atoi(info);

    info = strtok(NULL, ",");
    inventory[i].liveInv =  *info;

     info = strtok(NULL, ",");
    strcpy(inventory[i].productName, info);

    i++;
    }

    fclose(pFile);
   return 0;
JohnM.904
  • 1
  • 1
  • 1
    The way to debug such problems is to use a debugger. And for starters, always check each return value of `strtok` to validate that it did not fail. – kaylum Aug 14 '16 at 20:51
  • haven't you added an extra empty line in the end? as kaylum said, your code isn't robust to badly formatted lines. – Jean-François Fabre Aug 14 '16 at 20:56
  • How about starting to learn C? You asked a lot of much the same basic questions the last time (changing user-accounts). Get a C book, this no tutoring resp. homework helper site. You are expected to do some effort on your own first. – too honest for this site Aug 14 '16 at 20:58
  • @kaylum I ran the function through the debugger, and it seemed to get all the information correctly. I think the while loop could be a problem because it seems to keep going after the data has ran out. Do you see anything there that could be wrong with the code? – JohnM.904 Aug 14 '16 at 21:02
  • Are you working as a team, or is everyone in your class having problems? There's [Importing a CSV list to an array structure in C](http://stackoverflow.com/questions/38921732/) that has the same CSV data file. And there's [Appending a character and a string variable to a file in CSV format](http://stackoverflow.com/questions/38820708/) which has almost the same data except for a typo and an extra line. – Jonathan Leffler Aug 14 '16 at 22:01

1 Answers1

0

You use MAX_INVENTORY as both the number of structs AND as the buf length. There is no connection between the two. you need to define the maximum line length apart from the number of structs.

If you have, say, 10 structs, but the line length is about 30, then you read only a fraction of the line, the parsing is disrupted, and a carsh can occure.

Israel Unterman
  • 13,158
  • 4
  • 28
  • 35
  • good find! not to mention it is uselss to `char *buf = malloc(MAX_INVENTORY);` when `char buf[MAX_INVENTORY];` would do nicely without risks of memory leaks. – Jean-François Fabre Aug 14 '16 at 20:58
  • @Israel Unterman I changed that up and still get a crash. I'm pretty sure the while loop isn't functioning properly. Do you see any problem in the way it's set up? – JohnM.904 Aug 14 '16 at 21:11
  • @JohnM.904 Update the question to show exactly what you changed. Otherwise it's easy to misunderstand what "that" you changed. And while you are at it, add the checks for the `strtok` return values and show that as well. It's not productive proceeding with your question until you do the basics. – kaylum Aug 14 '16 at 22:32