0

I am using this code to parse a .csv file in C. It works if the fields are in this format ENTRY1,ENTRY2,ENTRY3,ENTRY4 or even if there are commas here: ENTRY1, ENTRY2, ENTRY3, ENTRY4 However, if after an entry and before a comma there is a space, the program crashes. Like so: ENTRY1 ,ENTRY2,ENTRY3,ENTRY4 CODE:

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

int main()
{
        FILE *input_fp =fopen("file", "r");
        char buf[100];

        while (fgets(buf, sizeof buf, input_fp) != NULL) {
                char field1[30], field2[30], field3[30], field4[30];
#define VFMT " %29[^ ,\n\t]" //Defines limit of one entry to be 30 characters
                int n; // Use to check for trailing junk

                if (4 == sscanf(buf, VFMT "," VFMT "," VFMT "," VFMT " %n", field1, field2,
                                field3, field4, &n) && buf[n] == '\0') {
                        // Suspect OP really wants this wfield1th to be 1 more
                        if (printf("%s %s %s %s\n", field1, field2, field3, field4) < 0)
                                break;
                } else
                        break; // format error
        }
        fclose(input_fp);
return 0;
}

Example run: file contains:

ENTRY1, ENTRY2, ENTRY3, ENTRY4
ENTRY5,ENTRY6,ENTRY7,ENTRY8
ENTRY5 , ENTRY6, ENTRY7, ENTRY8
ENTRY1, ENTRY2, ENTRY3, ENTRY4

the output is:

ENTRY1 ENTRY2 ENTRY3 ENTRY4
ENTRY5 ENTRY6 ENTRY7 ENTRY8

it stops before concluding the third line and exits.

  • 2
    `void main`? Surely `int main` – Ed Heal Nov 14 '19 at 15:05
  • Oops. I usually use `void main`. I tried using a `return 0;` and `int main` and the results are the same. Thus, I edited the code to ensure other people don't get confused as if this was the error. Thanks! – alexandrenf Nov 14 '19 at 15:07
  • 1
    While it may not appear to matter here, C only offers a few valid `main()` definitions and `void main()` isn't one of them. [You can read more about that here.](https://stackoverflow.com/a/2108208/5893772) – Reticulated Spline Nov 14 '19 at 15:08
  • 1
    So, what's your question? This: `VFMT ","` won't match if there's a space before the comma. – mustaccio Nov 14 '19 at 15:09
  • Ohhh, I see the problem now. However, what is another way that I can parse the file? I need to exclude all spaces and commas from results – alexandrenf Nov 14 '19 at 15:10
  • suggest using a loop with `strtok()` where the delimiters are " \n\t" (notice the space) and forget about using `sscanf()` – user3629249 Nov 15 '19 at 16:49
  • regarding: `FILE *input_fp =fopen("file", "r");` always check for errors. I.E. `if( ! input_fp ) { perror( "fopen for file failed" ); exit( EXIT_FAILURE ); }` where `exit()` and EXIT_FAILURE are exposed via: `#include ` – user3629249 Nov 15 '19 at 16:53

1 Answers1

0

Going by your example and the expected output, I would prefer to use fgetc to process the input character in place.

This would allow us to parse and reject unwanted characters,thus avoiding the overhead of storing data- as in fgets and then parsing the stored data.

The result (fputc)could be directed to a file as well...

The code would look like this.....Hope this helps......

#include <stdio.h>

int main()
{
  FILE *input_fp =fopen("file", "r");
  int c;
  while ((c=fgetc(input_fp)) != EOF)
   {
      if(c == ' ' || c == '\t')//if space or tab
        ;                      //do nothing
      else
        fputc(c,stdout); //print c
   }
  fclose(input_fp);
  return 0;
}
Lily AB
  • 219
  • 2
  • 5