| | |
Parsing a csv file in C
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2007
Posts: 2
Reputation:
Solved Threads: 0
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.
****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.
c Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXFLDS 200 /* maximum possible number of fields */ #define MAXFLDSIZE 32 /* longest possible field + 1 = 31 byte field */ void parse( char *record, char *delim, char arr[][MAXFLDSIZE],int *fldcnt) { char*p=strtok(record,delim); int fld=0; while(*p) { strcpy(arr[fld],p); fld++; p=strtok('\0',delim); } *fldcnt=fld; } int main(int argc, char *argv[]) { char tmp[1024]={0x0}; int fldcnt=0; char arr[MAXFLDS][MAXFLDSIZE]={0x0}; int recordcnt=0; FILE *in=fopen(argv[1],"r"); /* open file on command line */ if(in==NULL) { perror("File open error"); exit(EXIT_FAILURE); } while(fgets(tmp,sizeof(tmp),in)!=0) /* read a record */ { int i=0; recordcnt++; printf("Record number: %d\n",recordcnt); parse(tmp,",",arr,&fldcnt); /* whack record into fields */ for(i=0;i<fldcnt;i++) { /* print each field */ printf("\tField number: %3d==%s\n",i,arr[i]); } } fclose(in); return 0; }
Last edited by Ancient Dragon; Nov 21st, 2007 at 5:31 pm. Reason: add line numbers
line 12: should be this:
>>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.
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.
> 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!)
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!)
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.
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.
![]() |
Similar Threads
- Reading in a *.csv file and loading the data into an Array (Java)
- Parsing CSV file in partcular format (Java)
- Remove quotation marks from a CSV file (C#)
- Need Help Reading a csv file created from MSExcel (C)
- parsing csv file (PHP)
Other Threads in the C Forum
- Previous Thread: How display a page using assambly code? in TC
- Next Thread: sorting and class data
| Thread Tools | Search this Thread |
#include * append array arrays asterisks binarysearch calculate changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic execv feet fflush fgets file fork forloop framework function getlasterror givemetehcodez grade gtkwinlinux hacking histogram inches include incrementoperators input intmain() iso kernel keyboard km license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix microsoft motherboard mqqueue number oddnumber odf opendocumentformat opensource overwrite owf pattern pdf performance pointer posix problem probleminc process program programming radix recursion recv recvblocked research reversing scanf scripting segmentationfault sequential socket socketprograming standard string structures systemcall testing threads turboc unix user variable voidmain() wab whythiscodecausesegmentationfault windowsapi






