简体   繁体   中英

Read more than 1 line csv file in C

i am trying to read more than 1 line csv file,i've been sucessful to read 1 line csv file,can some one give me and example to read more than 1 line csv file? here's the input sample :

Nama,Gaji,Zakat,Gaji Bersih
Ali,1234567,,
Sofyan,2345678,,
Kholimi,3456789,,

here's my 1 line csv read source code :

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

int main(void)
{
    char nama[100], gaji[100], zakat[100], bersih[100];

    FILE* f = fopen("2.csv", "r");

    fscanf(f, "%s %s %s %s", nama, gaji, zakat, bersih);

    //delete comma
    size_t len = strlen(gaji);
    size_t len1 = strlen(nama);
    size_t len2 = strlen(zakat);

    gaji[len - 1] = '\0';
    nama[len1 - 1] = '\0';
    zakat[len2 - 1] = '\0';
    //delete comma

    printf("%s \t %s \t %s \t %s \n", nama, gaji, zakat, bersih);
    fclose(f);
    return 0;
}

If you want to read multiple lines with fscanf because they have the same format, I'd read the whole line with fgets and parse it with sscanf , it easier to deal with format errors this way:

int main(void)
{
    char nama[100], gaji[100], zakat[100], bersih[100];

    FILE* f = fopen("2.csv", "r");

    if(f == NULL)
    {
        fprintf(stderr, "Cannot open file\n");
        return 1;
    }

    char line[1024];
    size_t lineno = 0;
    while(fgets(line, sizeof line, stdin))
    {
        lineno++;
        if(sscanf(line, "%99s,%99s,%99s,%99s", nama, gaji, zakat, bersih) != 4)
        {
            fprintf(stderr, "format error on line %zu\n", lineno);
            continue;
        }

        printf("line %zu: name: %s, gaji: %s, zakat: %s, bersih: %s\n", lineno, nama, gaji, zakat, bersih);
    }

    fclose(f);
    return 0;
}

You could also use strtok to parse the lines of the CSV, for example when you don't know how many columns there are or the columns have multiple empty spaces, etc:

int main(void)
{
    FILE* f = fopen("2.csv", "r");

    if(f == NULL)
    {
        fprintf(stderr, "Cannot open file\n");
        return 1;
    }

    char line[1024];
    size_t lineno = 0;
    const char *delim = ",\n";
    while(fgets(line, sizeof line, stdin))
    {
        lineno++;

        char *token = strtok(line, delim);
        if(token == NULL)
        {
            fprintf(stderr, "format error on line %zu\n", lineno);
            continue;
        }

        printf("line %zu:");

        do {
            printf("-%s- ", token);
        } while((token = strtok(NULL, delim)));

        putchar('\n');
    }

    fclose(f);
    return 0;
}

For a line "a,b,c,d,e" it would print "line 1: -a- -b- -c- -d- " .

You are incorrectly reading the CSV, even regardless of the only-one-line issue.

CSV is a format in which fields are separated by commas (not spaces, like your code assumes), and records are separated by newline characters; but it's actually much more complicated than that: There are escapes, and you can have commas and/or newlines within a single datum. The format is officially documented in RFC 4180 .

But you obviously shouldn't have to reinvent the wheel and write your own CSV parsing code. Instead, use one of the publicly-available CSV parser libraries, such as semitrivial's csv_parser or the venerable libcsv .

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

int main(void)
{
    char nama[100], gaji[100], zakat[100], bersih[100];

    FILE* f = fopen("2.csv", "r");

    while(fscanf(f, "%s %s %s %s", nama, gaji, zakat, bersih)!=EOF){

        //delete comma
        size_t len = strlen(gaji);
        size_t len1 = strlen(nama);
        size_t len2 = strlen(zakat);

        gaji[len - 1] = '\0';
        nama[len1 - 1] = '\0';
        zakat[len2 - 1] = '\0';
        //delete comma

        printf("%s \t %s \t %s \t %s \n", nama, gaji, zakat, bersih);
    }

    fclose(f);
    return 0;
}

do you want to end up with something like this?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM