Encerrado

CSV Reader in C programming

I have the start of a simple CSV C program, that reads the file, parses it, then displays it. The code is 80% done but there is a litte bit of code that needs to be added to read the file and there may be other errors.

I would like someone to finish the program so it works without errors.

## Deliverables

This is the code I currently have:

#include <stdio.h>

#include "stdafx.h"

int main(int argc, char ** argv){

? int c;

? FILE * fp;

? if(argc < 2){

? ? ? printf("Usage:\n\t%s filename\n",argv[0]);

? ? ? return -1;

? }

? if((fp = fopen(argv[1],"rb")) == NULL){

? ? ? printf("can't open %s\n",argv[1]);

? ? ? return -2;

? }

? while((c = fgetc(fp)) != EOF){

#define MAX_LINE_LEN? ? 1024*512? // 1/2 mega byte, should be more than sufficient */

? ? ? char line[MAX_LINE_LEN];

? ? int len=0;

? ? int cnt_of_fields=0;

? ? char *p;

? ? while( (c=GET_A_CHAR_FROM_FILE(fp))!=EOF){

? ? ? ? ? ? ? ? ? ? switch(c)

? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? case '"':

? ? ? ? ? ? ? ? ? ? case '\'':

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // parse a quoted string, ignore it for now */

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break;

? ? ? ? ? ? ? ? ? ? case ',':

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ++cnt_of_fields; // a comma signal end of previous field and begining of next fields */

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? line[len++]='\0';

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if(len==MAX_LINE_LEN){

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? fprintf(stderr, "Line too long\n");

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? exit(-1);

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break;

? ? ? ? ? ? ? ? ? ? case '\n':

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ++cnt_of_fields; // a EOL is end of record, and at the same time end of field */

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? line[len++]='\0';

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // make a copy of the line in the heap, note strdup or strcpy won't work in our case */

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? p = (char *)malloc(len);

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? memcpy(p, line, len); // now all the fields in the record are stored in p[ ] */

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? add_a_record( p );

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break;

? ? ? ? ? ? ? ? ? ? default:

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? line[len++] = c;

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if(len==MAX_LINE_LEN){

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? fprintf(stderr, "Line too long\n");

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? exit(-1);

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? }

? ? }

? ? ? printf( "%s", line );

? }

? fclose(fp);

? return 0;

}

Habilidades: Programação C, Engenharia, Microsoft, MySQL, PHP, Gestão de projetos, Arquitetura de software, Teste de Software, Área de trabalho do Windows

Veja mais: the c programming, switch c programming, switch case c programming, string programming, string in c programming, string c programming, start programming, programming errors, programming define, printf c programming, parse programming, or in c programming, needs for c# programming, malloc c programming, in c programming, & in c programming, if c programming, heap programming, for c programming, fopen c programming

Acerca do Empregador:
( 70 comentários ) Bristol, United Kingdom

ID do Projeto: #3631216

48 freelancers estão ofertando em média $14 para esse trabalho

MuktoSoftware

See private message.

$21.25 USD in 3 dias
(455 Comentários)
7.5
MosesImmanuvel

See private message.

$21.25 USD in 3 dias
(59 Comentários)
5.8
vw6868760vw

See private message.

$21.25 USD in 3 dias
(53 Comentários)
5.6
wangpretty

See private message.

$17 USD in 3 dias
(27 Comentários)
5.3
rogersilverstone

See private message.

$6.8 USD in 3 dias
(123 Comentários)
4.9
anurag7vw

See private message.

$12.75 USD in 3 dias
(69 Comentários)
4.9
zenecisvw

See private message.

$21.25 USD in 3 dias
(32 Comentários)
4.9
istepin

See private message.

$20.4 USD in 3 dias
(1 Comentário)
4.8
urmate

See private message.

$17 USD in 3 dias
(43 Comentários)
4.6
netgurusatti

See private message.

$21.25 USD in 3 dias
(13 Comentários)
4.8
damcclos

See private message.

$21.25 USD in 3 dias
(28 Comentários)
4.5
tarinmahmood

See private message.

$17 USD in 3 dias
(34 Comentários)
4.4
rajivk2k

See private message.

$12.75 USD in 3 dias
(22 Comentários)
4.4
dsysoft

See private message.

$13.6 USD in 3 dias
(32 Comentários)
4.3
bucktracker

See private message.

$20.4 USD in 3 dias
(26 Comentários)
4.2
arhvw

See private message.

$12.75 USD in 3 dias
(5 Comentários)
3.8
sthustfo

See private message.

$21.25 USD in 3 dias
(16 Comentários)
3.8
gworroll

See private message.

$17 USD in 3 dias
(42 Comentários)
3.7
pddthinh

See private message.

$12.75 USD in 3 dias
(20 Comentários)
3.6
workgood

See private message.

$10.2 USD in 3 dias
(6 Comentários)
3.0