Link to home
Start Free TrialLog in
Avatar of richardoc
richardoc

asked on

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.

Thanks,

Richard

ASKER CERTIFIED SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of richardoc
richardoc

ASKER

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.



Since strsep() is not 100% portable to all C compilers, I suggest this identical implementation:
http://www.greatsnakes.com/Sepal/d4/d0/strsep_8c-source.html
Is strsep() used in the same way as strtok() ?

E.G:

char * currentElement;

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


Thanks.
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.

Paul
>>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.

Paul