0

I'm reading a tab delimited csv file with fscanf.

The file was obtained from an SQL database where some columns were NULL. Accordingly, there are places where there is nothing in csv between tabs, and then fscanf goes astray and incorrectly produces a formatted record.

Is there any elegant way to check for missing values in a formatted entry?

struct Path {
    unsigned int objectId, parentObjectId, changeId, prevId, nextId;
    unsigned short regionCode, areaCode, cityCode, placeCode, planCode, streetCode;
    char updateDate[10], startDate[10], endDate[10];
    bool isActive;
    char path[300];
}

while (!feof(ah))
{
    path = (struct Path*) malloc(sizeof(struct Path));
    fscanf(ah, "%u  %u  %u  %u  %u  %u  %u  %u  %u  %u  %u  %s  %s  %s  %u  %s", 
        &path->objectId, &path->parentObjectId, &path->changeId, 
        &path->regionCode, &path->areaCode, &path->cityCode, &path->placeCode, &path->planCode, &path->streetCode,
        &path->prevId, &path->nextId,
        &path->updateDate, &path->startDate, &path->endDate,
        &path->isActive, &path->path);
    free(path);
}
fclose(ah);
  • 4
    Please read [Why is “while( !feof(file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feoffile-always-wrong) – Some programmer dude Sep 02 '23 at 14:41
  • 5
    As for your problem, please try to get a library to handle CSV files for you. The files might seem simple to handle, but then something like this turns up and suddenly everything becomes so much harder. And this is not the only non-trivial case with such files! – Some programmer dude Sep 02 '23 at 14:42
  • 2
    `fscanf` is not well suited to your purpose. (Some say it's not suited to *any* purpose.) When `fscanf` consumes whitespace, it consumes arbitrary *runs* of whitespace -- for example, pairs of consecutive tabs. Also, it mostly does not distinguish between spaces, tabs, newlines, and other whitespace characters. Get a third-party parser or maybe write your own -- without relying on `fscanf`. – John Bollinger Sep 02 '23 at 16:59

0 Answers0