I always like to use the strtok() or strtok_r() function to parse the file. (or prefer to use some csv library).
But just for fun, I wrote the code, maybe you like it, I do not send the code in my answer, but I check @ codepad for output, it works only for a certain format.
Using strtok ()
The correct approach looks to me as follows:
int main(){ // while(fp, csv, sizeof(csv)){ // First read into a part of file into buffer char csv[] = "\"MARY\",\"PATRICIA\",\"LINDA\",\"BARBARA\",\"ELIZABETH\""; char *name = "", *parse = csv; while(name = strtok(parse, "\",")){ printf(" %s\n", name); parse = NULL; } return 0; } // end while
Mark the code page for the output:
MARY PATRICIA LINDA BARBARA ELIZABETH
As I suggest in the second code, draw an outer loop to read the lines from the file into a temporary buffer, and then apply the strtok () code as shown above: while(fgets(fp, csv, sizeof(csv))){ use strtok code}
Grijesh chauhan
source share