0

I have a CSV file with the following data

15278349,567892,3456,9742.30,CASH-WITHDRAWAL-200.00,DIRECT-DEBIT-200.00
64328975,632489,7562,98734.65,CHEQUE-DEPOSIT-100.00,CASH-DEPOSIT-424.54

etc...

How would I print to screen the individual lines instead of just the top line??

Sam

Edited by berwick53: n/a

3
Contributors
2
Replies
4
Views
6 Years
Discussion Span
Last Post by Narue
0

Use fgets to read each line, then your favorite CSV reading algorithm to separate the fields. Here's one easy way to do it if you're not using a complex form of CSV:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    FILE *in = fopen("data.txt", "r");
    char line[BUFSIZ];

    if (!in) {
        perror(NULL);
        return EXIT_FAILURE;
    }

    /* Poor main's CSV input (doesn't consider embedded commas) */
    while (fgets(line, sizeof line, in) != NULL) {
        char field[BUFSIZ];
        int offset = 0;
        int n;

        while (sscanf(line + offset, "%[^,\n]%n", field, &n) == 1) {
            printf(">%s<\n", field);
            offset += n + 1;
        }

        puts("");
    }

    fclose(in);

    return EXIT_SUCCESS;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.