Parsing a csv file in C

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2007
Posts: 2
Reputation: matt nagel is an unknown quantity at this point 
Solved Threads: 0
matt nagel matt nagel is offline Offline
Newbie Poster

Parsing a csv file in C

 
0
  #1
Nov 21st, 2007
Hi there, I am fairly new to programming and trying create a program to parse csv data into an array. I have been following an example I found on this site but I am running into a segmentation fault (even if I copy code directly). I am fairly sure the problem lies in the while loop, I just cant see it.


****Sample Csv Data****
1.0235, 2.1112, 1.9972
3.0139, 1.1876, 2.91785
1.9295, 2.1322, 2.4821

I realize its probably overkill for my purpose so if anyone could point me to a simpler solution that would be appreciated too.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define MAXFLDS 200 /* maximum possible number of fields */
  5. #define MAXFLDSIZE 32 /* longest possible field + 1 = 31 byte field */
  6.  
  7. void parse( char *record, char *delim, char arr[][MAXFLDSIZE],int *fldcnt)
  8. {
  9. char*p=strtok(record,delim);
  10. int fld=0;
  11.  
  12. while(*p)
  13. {
  14. strcpy(arr[fld],p);
  15. fld++;
  16. p=strtok('\0',delim);
  17. }
  18. *fldcnt=fld;
  19. }
  20.  
  21. int main(int argc, char *argv[])
  22. {
  23. char tmp[1024]={0x0};
  24. int fldcnt=0;
  25. char arr[MAXFLDS][MAXFLDSIZE]={0x0};
  26. int recordcnt=0;
  27. FILE *in=fopen(argv[1],"r"); /* open file on command line */
  28.  
  29. if(in==NULL)
  30. {
  31. perror("File open error");
  32. exit(EXIT_FAILURE);
  33. }
  34. while(fgets(tmp,sizeof(tmp),in)!=0) /* read a record */
  35. {
  36. int i=0;
  37. recordcnt++;
  38. printf("Record number: %d\n",recordcnt);
  39. parse(tmp,",",arr,&fldcnt); /* whack record into fields */
  40. for(i=0;i<fldcnt;i++)
  41. { /* print each field */
  42. printf("\tField number: %3d==%s\n",i,arr[i]);
  43. }
  44. }
  45. fclose(in);
  46. return 0;
  47. }
Last edited by Ancient Dragon; Nov 21st, 2007 at 5:31 pm. Reason: add line numbers
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,502
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1479
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Parsing a csv file in C

 
0
  #2
Nov 21st, 2007
line 12: should be this: while(p) Your program gets segment violation when strtok() returns NULL.

>>I realize its probably overkill for my purpose so if anyone could point me to a simpler solution that would be appreciated too.
In C language that's about the best anyone can do.
Last edited by Ancient Dragon; Nov 21st, 2007 at 5:34 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: Parsing a csv file in C

 
0
  #3
Nov 22nd, 2007
I am not sure if this is needed or not, but most CSV files have a header record, which is essentially the names of the columns separated by commas. If you are parsing an actual CSV file, you will need to take that into account also.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Parsing a csv file in C

 
0
  #4
Nov 22nd, 2007
> I am not sure if this is needed or not, but most CSV files have a header record, which is essentially the names of the columns separated by commas. If you are parsing an actual CSV file, you will need to take that into account also.

That may be true for some, but the vast majority of CSV files in use have no such thing. Unlike something like an XML file, CSV files have no way to tell you what they contain. The reading program must know what to expect when reading any CSV. Even the assumption that CSV is textual data, or fields separated by commas or tabs, and other stuff, is entirely that, assumption.

If you like I'll dig up an old CSV parser I wrote that can handle anything and port it to C for you. (Remember, it can read and write any CSV file, but, again, you must know what that CSV file contains for it to be useful!)
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 2
Reputation: matt nagel is an unknown quantity at this point 
Solved Threads: 0
matt nagel matt nagel is offline Offline
Newbie Poster

Re: Parsing a csv file in C

 
0
  #5
Nov 22nd, 2007
Ahhh thanks for that, a silly mistake.

Duoas if you have it lying around Id love to take a look

Thanks in advance.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Parsing a csv file in C

 
0
  #6
Nov 28th, 2007
Hey there, if you're still interested, don't give up.

I wrote it originally in Object Pascal, and I used a number of very powerful container classes that don't exist in C (...obviously). So I've been coding a replacement for them also... (It's kind of boring actually...)

I should be done pretty soon. Once I give it some basic testing I'll wrap the whole thing up for you and respond here again.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC