• C

Parse strings from CSV file

I have to parse a CSV file (in an embedded application), which contains strings and numbers.

I am currently getting numbers back using the following method:

Firstly, I load the file contents into an unsigned int array called RecipeData... then somthing like:

char * currentElement;

currentElement = strtok (RecipeData,",");
while (currentElement != NULL)
     RecipeDataStructure[0].recipeNo =  currentElement;
     currentElement = strtok (NULL, " ,");

My question is, how do I deal with strings?
Do the strings need to be enclosed in quotes within the CSV file to make them identifiable?

Can I use a simlar method to that which I use for reading int's:
 E.G: RecipeDataStructure[0].recipeName =  currentElement;
where RecipeDataStructure[0].recipeName is of type char[].  I believe the above statement will not compile, I'm only using it as an example of what I would like to achieve - something like it.



Who is Participating?
Jaime OlivaresConnect With a Mentor Software ArchitectCommented:
Will be better if you post your Recipe structure. But you have many alternatives.
Assumming a string member of recibe is something like this

struct RecipeDataStructure {
/* some members */
     char recipeName[100];
     int recipeNo;

you could use strcpy or strncpy, something like this:

char * currentElement;
int field = 0;   /* field identifier */
currentElement = strtok (RecipeData,",");
while (currentElement != NULL)
     RecipeDataStructure[0].recipeNo =  currentElement;
     currentElement = strtok (NULL, " ,");

     if (field==0)
         strcpy( RecipeDataStructure[0].recipeName, currentElement);
     else if (field==1)
          RecipeDataStructure[0].recipeNo = atoi(currentElement);

richardocAuthor Commented:
My recipe structure is as follows:

typedef struct RecipeData_typ
      UINT                  recipeNo;
      STRING                  recipeName[20];
      STRING                  recipeCode[10];
      STRING                  recipeDesc[30];

      UINT                  comp1p;
      UINT                  comp1m;
      UINT                  comp2p;
      UINT                  comp2m;
      UINT                  comp3p;
      UINT                  comp3m;
      UINT                  comp4p;
      UINT                  comp4m;
      UINT                  comp5p;
      UINT                  comp5m;
      UINT                  comp6p;
      UINT                  comp6m;
      UINT                  comp7p;
      UINT                  comp7m;
      UINT                  comp8p;
      UINT                  comp8m;
      UINT                  comp9p;
      UINT                  comp9m;

      STRING                  companyName[20];

} RecipeData_typ;
Actually, strtok() is a poor tool to use on CSV lists, because it doesn't track empty values.
You should use strsep() instead.   Yet neither handles quoted strings (containing commas
or quoted quotes) particularly well.

Live webcast with Pinal Dave

Pinal Dave will teach you tricks to help identify the real root cause of database problems rather than red herrings. Attendees will learn scripts that they can use in their environment to immediately figure out their performance Blame Shifters and fix them quickly.

Jaime OlivaresSoftware ArchitectCommented:
Since strsep() is not 100% portable to all C compilers, I suggest this identical implementation:
richardocAuthor Commented:
Is strsep() used in the same way as strtok() ?


char * currentElement;

currentElement = strsep (RecipeData,",");
while (currentElement != NULL)
     RecipeDataStructure[0].recipeNo =  currentElement;
     currentElement = strsep (NULL, " ,");

This works well:

char token[100];
int pos = 0;

// Some implementations will deliver '2' here so try it both ways.
while ( sscanf ( &line[pos], "%[^,]%*n", token, &pos ) == 1 )
  // token contains the next token in the line in string form.
  if ( line[pos] == ',' ) pos += 1;

This enumerates each field in the file as a string. If you want to turn it into a number then use atoi or atol etc.

>>Do the strings need to be enclosed in quotes within the CSV file to make them identifiable?
It depends if any of your fields might contain commas. If they might then 1. Yes, you will need to enclose them in quotes and 2) my method above wont work.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.