Solved

Parse strings from CSV file

Posted on 2004-09-30
7
928 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
ID: 12191121
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
ID: 12191621
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
ID: 12193569
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12193721
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
ID: 12198217
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
ID: 12200150
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
ID: 12200168
>>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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

813 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

10 Experts available now in Live!

Get 1:1 Help Now