char - Parsing token strings in C -


i trying parse csv file in c. have each line of file scanned array called lines, works. then, check each character in line see if comma (44).

i having trouble last else statement, should start new token when there comma.

the first token of line read correctly, rest not (strange symbols/characters appear in output). tried removing '\0' statement, since i'm not sure needed it, have same problem. guessing kind of undefined behavior, not sure.

thanks!

//[rows = num strings] [max num chars per string] int max_len = 21; int num_strings = 12; char lines[num_strings][max_len];  //open file data = fopen("data.txt", "r");  //check if file opened correctly if (data == null) {      printf ("file did not open correctly.\n"); }  //parse each token char tokens[60][21]; int counter = 0; //read each line for(int i=0; i<num_strings; i++) {     //scan line lines[i]     fscanf(data, "%s", lines[i]);      printf("\nthis line = %s\n",lines[i]);      //read each char in line     for(int j=0; j<strlen(lines[i]); j++)     {         char *c = &lines[i][j];         //printf("current char of line: %c\n", c[0]);          //if it's not comma (or null character), add current token         if(c[0] != 44) {            tokens[counter][j] = c[0];         } else {//if is, terminate string , go next token             tokens[counter][j] = '\0';             printf("this token = %s\n",tokens[counter]);             counter++;         }     } } 

my suggestion draw diagram of strings, have line , you'll find first comma:

      .          1         2       .01234567890123456789012  -> |aaaa,bbb,cccccc,dddd,e\0       .    ^             j 

this tokens array:

          01234        counter |aaaa\0  

now increment counter j continue, next time have:

      .          1         2       .01234567890123456789012  -> |aaaa,bbb,cccccc,dddd,e\0       .        ^                 j 

and next line in tokens array be:

            01234 567                 |aaaa\0     counter |????? bbb\0  

not intended, right?

you should find way copy characters in token array.

may suggest if need fill token array, can rid of lines entirely , read file 1 character @ time?

also, suppose practice did not mention fact csv may contain comma within string:

  aaaa,"bb,bb",ccc 

has 3 field.


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -