1


I'm programming a simple program to implement a linked list using the programming language C.
The elements to be added in the list are given in a CSV-File.
I want each line to be safed in a struct. Which I can then add to the list.

The CSV-File looks like this:

datatype1;datatype2;datatype3;datatype4;datatype5;datatype6

there are some special chars in it.

My attempt now to read from the csv and safe it line by line in a struct looks like this:

void createListFromCSV(char *path_csv, struct listenElement **list)
{
    FILE *CSV = fopen(path_csv, "r");
    if (CSV == NULL)
    {
        exit(-1);
    }
    //datatype1;datatype2;datatype3;datatype4;datatype5;datatype6
    //%s,%s,%s,%s,%s,%s
    char read[150];
    const char delimiter[2] = ";";
    fgets(read, 150, CSV);
    while (!feof(CSV))
    {
        int counter = 0;
        dataElement newElement;
        char *token = strtok(read, delimiter);
        while (token != NULL)
        {
            switch (counter)
            {
                case 0:
                    strcpy(newElement.datatype1, token);
                    break;
                case 1:
                    strcpy(newElement.datatype2, token);
                    break;
                case 2:
                    strcpy(newElement.datatype3, token);
                    break;
                case 3:
                    strcpy(newElement.datatype4, token);
                    break;
                case 4:
                    strcpy(newElement.datatype5, token);
                    break;
                case 5:
                    strcpy(newElement.datatype6, token);
                    break;
                default:
                    printf("%s\n", "[!!] Error unknown part of the string");
                    break;
            }
            counter++;
            token = strtok(NULL, delimiter);
        }
        fgets(read, 150, CSV);
        putOnEndOfList(list, &newElement);
    }
    // delete the first Element of the list!
    deleteElement(list, "datatype5");
    fclose(CSV);
}


There have to be a much better way to read from the CSV and I researched, but nothing really worked for me in this example.

NOTICE: the datatypes also have some special chars !

This attempt works, but it is not working for other CSV-Files and is really unusable. I would appreciate some suggestion for a better solution :)

I'm not searching for a library. I want to implement it myself

MNCODE
  • 159
  • 3
  • 16
  • 1
    OT: https://stackoverflow.com/q/5431941/694576 – alk Oct 07 '17 at 14:43
  • 1
    First: dont use feof() this way: https://stackoverflow.com/q/5431941/905902 Second: strtok() is **unusable** (if the csv-file contains empty fields) – wildplasser Oct 07 '17 at 14:44
  • Are you after a different way of reading or storing any .csv's content? Or both :-) – alk Oct 07 '17 at 14:45
  • 1
    See http://stackoverflow.com/questions/32349263/c-regex-how-to-match-any-string-ending-with-or-any-empty-string/32351114#32351114 – Paul Ogilvie Oct 07 '17 at 14:46
  • 1
    If it's the switch which annoys you, think about using an array instead of a `struct`. – alk Oct 07 '17 at 14:46
  • @wildplasser and how do I deal with empty fields ? – MNCODE Oct 07 '17 at 14:50
  • @alk both reading and storing. And if I use an Array I also have the problem to tell which line it is, so i need a counter and a switch or not ? – MNCODE Oct 07 '17 at 14:51
  • You'd need a counter, sure. You wouldn't need the switch as the counter would serve as an index to the array. – alk Oct 07 '17 at 14:52
  • 1
    Construct a minimal state machine (DFA) which feeds some counters and pointers. – wildplasser Oct 07 '17 at 14:55
  • @Paul Ogilvie It helped me, but it was hard to read. It cant deal with empty fields am I right ? – MNCODE Oct 07 '17 at 15:01
  • Yes it does handle empty fields. It returns an empty field/string. The example also has an empty fields. Just run the example. – Paul Ogilvie Oct 07 '17 at 15:03
  • @wildplasser but strtok gives me an empty string if there is something like ;; or am I wrong ? So it could deal with empty fields, but not with less fields. – MNCODE Oct 07 '17 at 15:03
  • 3
    No,strtok() will treat `;;` as **one** separator; and there will be too few fields filled when the`\n` is seen. – wildplasser Oct 07 '17 at 15:06
  • Okay thanks, but how can I handle the fist line of the CSV for something useful. It do not belong to the data I want to read. Is there any way to use them to define/name the datatypes of the structure. Or if not how can i easily skip this or just start reading in the second line of the file ? – MNCODE Oct 07 '17 at 15:10
  • 1
    If you want to skip the header line: just remember that you have seen the first`\n` (using a flag or line counter), and suppress assignment before it is seen. – wildplasser Oct 07 '17 at 15:21
  • The stuff mentioned in the answers given to the question mentioned in the reason I closed this question for, should give just quiet some examples how to approach your issue. Study the code, understand it and in the best case make it better. ;-) – alk Oct 07 '17 at 15:23
  • 1
    The question how to write a generic .csv parser is way to broad for SO. – alk Oct 07 '17 at 15:24

0 Answers0