C: CSV implementation


 
Thread Tools Search this Thread
Top Forums Programming C: CSV implementation
# 1  
Old 11-12-2014
C: CSV implementation

I have this code from a programming book:

Code:
#include <stdio.h>
#include <string.h>

char buf[200];          /* input line buffer */
char* field[20];        /* fields */
char* unquote( char* );
        /* csvgetline: read and parse line, return field count */
        /* sample input: "LU",86.25,"11/4/1998","2:19PM",+4.0625,"abc" */
int csvgetline( FILE* fin )
{
        int nfield;
        char *p, *q;
                        /* spacer */
        if( fgets( buf, sizeof( buf ), fin ) == NULL )
                return -1;
        nfield = 0;
        for (q = buf; (p=strtok(q, ",\n\r")) != NULL; q = NULL)
                field[nfield++] = unquote(p);
        return nfield;
}

/* unquote: remove leading and trailing quote */
char* unquote( char *p )
{
        if( p[0] == '"' ) {
                if( p[strlen(p)-1] == '"' )
                        p[strlen(p)-1] = '\0';
                p++;
        }
        return p;
}

extern char* field[];

/* csvtest main: test csvgetline function */
int main( int argc, char* argv[] )
{
        int i, nf;
        FILE* fp;

        if( argc < 2 ) 
                fp = stdin;
        else
                fp = fopen( argv[1], "r" );

        while(( nf=csvgetline( fp )) != -1 )
                for( i=0 ; i<nf ; i++ )
                        printf( "field[%d] = `%s'\n", i, field[i] );

        return 0;
}

How would I test this code on a file on the command line? I believe you use something like a.out? This file name is csvgetline.c
The book also states that there are problems with this implementation, would anyone know by just looking at it?
# 2  
Old 11-13-2014
Quote:
Originally Posted by totoro125
How would I test this code on a file on the command line? I believe you use something like a.out? This file name is csvgetline.c
I'm no C programmer, but I think I know some basics Smilie

First of all, you'll need to compile this code, try
Code:
gcc csvgetline.c  #produces executable file a.out

or
Code:
gcc -o csvgetline csvgetline.c  #produces executable file csvgetline

#invoke executable to read from standard input
Code:
./a.out OR ./csvgetline

Example:
Code:
$ ./a.out
"LU",86.25,"11/4/1998","2:19PM",+4.0625,"abc"
field[0] = `LU'
field[1] = `86.25'
field[2] = `11/4/1998'
field[3] = `2:19PM'
field[4] = `+4.0625'
field[5] = `abc'
^C
$

#invoke executable to read from a file
Code:
./a.out file.csv OR ./csvgetline file.csv

Example:
Code:
$ ./a.out file.csv
field[0] = `LU'
field[1] = `86.25'
field[2] = `11/4/1998'
field[3] = `2:19PM'
field[4] = `+4.0625'
field[5] = `abc'
$

Quote:
Originally Posted by totoro125
The book also states that there are problems with this implementation, would anyone know by just looking at it?
Not sure, but I'd say it's
1. the processing of pre-defined number of fields (?) even if the actual number of fields exceeds that limit and thus producing wrong output
I don't know why, but during some testing I found out it will handle 24 fields, but fail when there are >25 fields.
2. the processing of pre-defined line length even if the actual line length exceeds that limit and thus producing wrong output
which I'd summarize as "no error checking".

Demo for "too many fields":
Code:
$ ./a.out test.csv #file with 26 fields; A,B,C,D,...
field[0] = `РҠE'
field[1] = `ҠE'
field[2] = `ҠE'
field[3] = `E'
field[4] = `E'
field[5] = `F'
field[6] = `G'
field[7] = `H'
field[8] = `I'
field[9] = `J'
field[10] = `K'
field[11] = `L'
field[12] = `M'
field[13] = `N'
field[14] = `O'
field[15] = `P'
field[16] = `Q'
field[17] = `R'
field[18] = `S'
field[19] = `T'
field[20] = `U'
field[21] = `V'
field[22] = `W'
field[23] = `X'
field[24] = `Y'
field[25] = `Z'
$

Demo for "too long line":
Code:
$ ./a.out file.csv #file with 8 fields (8 x A..Z), line length 216 characters 
field[0] = `ABCDEFGHIJKLMNOPQRSTUVWXYZ'
field[1] = `ABCDEFGHIJKLMNOPQRSTUVWXYZ'
field[2] = `ABCDEFGHIJKLMNOPQRSTUVWXYZ'
field[3] = `ABCDEFGHIJKLMNOPQRSTUVWXYZ'
field[4] = `ABCDEFGHIJKLMNOPQRSTUVWXYZ'
field[5] = `ABCDEFGHIJKLMNOPQRSTUVWXYZ'
field[6] = `ABCDEFGHIJKLMNOPQRSTUVWXYZ'
field[7] = `ABCDEFGHIJ'  # note this "cut" at exactly 200th character
field[0] = `KLMNOPQRSTUVWXYZ'
$

This User Gave Thanks to junior-helper For This Post:
# 3  
Old 11-13-2014
It also can't handle escaped things like "this is a string with a \" mark inside it" But that may not be a bug if you don't consider that valid, since it isn't actually causing a crash.

Basically, they left out all the error checking for brevity.
This User Gave Thanks to Corona688 For This Post:
# 4  
Old 11-13-2014
Quote:
Originally Posted by junior-helper
I'm no C programmer, but I think I know some basics Smilie

First of all, you'll need to compile this code, try
Code:
gcc csvgetline.c  #produces executable file a.out

or
Code:
gcc -o csvgetline csvgetline.c  #produces executable file csvgetline

#invoke executable to read from standard input
Code:
./a.out OR ./csvgetline

Example:
Code:
$ ./a.out
"LU",86.25,"11/4/1998","2:19PM",+4.0625,"abc"
field[0] = `LU'
field[1] = `86.25'
field[2] = `11/4/1998'
field[3] = `2:19PM'
field[4] = `+4.0625'
field[5] = `abc'
^C
$

#invoke executable to read from a file
Code:
./a.out file.csv OR ./csvgetline file.csv

Example:
Code:
$ ./a.out file.csv
field[0] = `LU'
field[1] = `86.25'
field[2] = `11/4/1998'
field[3] = `2:19PM'
field[4] = `+4.0625'
field[5] = `abc'
$

Not sure, but I'd say it's
1. the processing of pre-defined number of fields (?) even if the actual number of fields exceeds that limit and thus producing wrong output
I don't know why, but during some testing I found out it will handle 24 fields, but fail when there are >25 fields.
2. the processing of pre-defined line length even if the actual line length exceeds that limit and thus producing wrong output
which I'd summarize as "no error checking".

Demo for "too many fields":
Code:
$ ./a.out test.csv #file with 26 fields; A,B,C,D,...
field[0] = `РҠE'
field[1] = `ҠE'
field[2] = `ҠE'
field[3] = `E'
field[4] = `E'
field[5] = `F'
field[6] = `G'
field[7] = `H'
field[8] = `I'
field[9] = `J'
field[10] = `K'
field[11] = `L'
field[12] = `M'
field[13] = `N'
field[14] = `O'
field[15] = `P'
field[16] = `Q'
field[17] = `R'
field[18] = `S'
field[19] = `T'
field[20] = `U'
field[21] = `V'
field[22] = `W'
field[23] = `X'
field[24] = `Y'
field[25] = `Z'
$

Demo for "too long line":
Code:
$ ./a.out file.csv #file with 8 fields (8 x A..Z), line length 216 characters 
field[0] = `ABCDEFGHIJKLMNOPQRSTUVWXYZ'
field[1] = `ABCDEFGHIJKLMNOPQRSTUVWXYZ'
field[2] = `ABCDEFGHIJKLMNOPQRSTUVWXYZ'
field[3] = `ABCDEFGHIJKLMNOPQRSTUVWXYZ'
field[4] = `ABCDEFGHIJKLMNOPQRSTUVWXYZ'
field[5] = `ABCDEFGHIJKLMNOPQRSTUVWXYZ'
field[6] = `ABCDEFGHIJKLMNOPQRSTUVWXYZ'
field[7] = `ABCDEFGHIJ'  # note this "cut" at exactly 200th character
field[0] = `KLMNOPQRSTUVWXYZ'
$

Thank you! You have been such a great help to me lately ^^
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Back up implementation

Is there any command to take create back up of a file as soon as when it is created?If not is it possible to create something like that? (3 Replies)
Discussion started by: Sindhu R
3 Replies

2. UNIX for Advanced & Expert Users

Ipsec implementation

How can i implement Ipsec between two machines in linux_ ubuntu? any link?? suggestion?? (0 Replies)
Discussion started by: elinaz
0 Replies

3. UNIX for Dummies Questions & Answers

Lseek implementation

Hi everybody, i've been googling for ages now and gotten kinda desperate... The question, however, might be rather trivial for the experts: What is it exactly, i.e. physically, the POSIX function (for a file) "lseek" does? Does it trigger some kind of synchronization on disk? Is it just for the... (4 Replies)
Discussion started by: Humudituu
4 Replies

4. Linux

CAPWAP implementation

Hi I'm trying to implement CAPWAP protocol for my application.i'm able to configure my server side but i'm getting error at client(WTP) side as IOCTL error.while running the command #./WTP /mnt/cf/capwap/ : wlan2 Starting WTP... # WTP Loads... (0 Replies)
Discussion started by: ran789
0 Replies

5. Programming

Implementation of dup2

Hi all,I'm reading <Advanced programming in the UNIX environment>,that book asked the reader to implement a function which has same functions with dup2 without calling fcntl.Could anyone give me a tip?Any help will be appreciated.:) (8 Replies)
Discussion started by: homeboy
8 Replies

6. UNIX for Advanced & Expert Users

Malloc Implementation in C

Hey Guys Some of my friends have got together and we are trying to write a basic kernel similar to Linux. I am trying to implement the malloc function in C and I am using a doubly linked list as the primary data structure. I need to allocate memory for this link list (duh...) and I don't feel... (2 Replies)
Discussion started by: rbansal2
2 Replies

7. Programming

Malloc implementation in C

Hey Guys I am trying to implement the malloc function for my OS class and I am having a little trouble with it. I would be really grateful if I could get some hints on this problem. So I am using a doubly-linked list as my data structure and I have to allocate memory for it (duh...). The... (1 Reply)
Discussion started by: Gambit_b
1 Replies

8. Shell Programming and Scripting

Need help on AWK implementation

Hi, I am accepting a string from user. compare this output with the awk output as below... echo "\n\n\tDay : \c" read day awk '{ if($day == $2) { if ($mon == $1) { print "Yes" }}}' syslog.txt I am getting the follwoing error awk: Field $() is not correct. The input line... (5 Replies)
Discussion started by: EmbedUX
5 Replies

9. UNIX for Advanced & Expert Users

Implementation of ls - i command

It is possible for me to obtain the Inode of the path name using ls -i <pathname> command Can anyone tell me how its implemented... (3 Replies)
Discussion started by: ganapathy.psgit
3 Replies

10. UNIX for Dummies Questions & Answers

Shell Implementation

I want to implement my own simple multi tasking shell in Unix which will take care of redirection (<, >, >>) and piping. I am just unable to get a concrete idea of how exactly I have to start. I have several books...some are.. 1. Maurice Bach- Design Of Unix Operating System 2. Richard... (3 Replies)
Discussion started by: clickonline1
3 Replies
Login or Register to Ask a Question