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

Recommended Answers

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) { …
Jump to Post

All 2 Replies

use the new line character \n end of your print statement

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;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.