Solved

Parse strings from CSV file

Posted on 2004-09-30
7
907 Views
Last Modified: 2012-08-14
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

0
Comment
Question by:richardoc
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 125 total points
Comment Utility
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);
     field++;
}

0
 

Author Comment

by:richardoc
Comment Utility
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;
0
 
LVL 23

Expert Comment

by:brettmjohnson
Comment Utility
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.



0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
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
0
 

Author Comment

by:richardoc
Comment Utility
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.
0
 
LVL 16

Expert Comment

by:PaulCaswell
Comment Utility
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
0
 
LVL 16

Expert Comment

by:PaulCaswell
Comment Utility
>>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
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now