4

I am Currently trying to read a .csv file into an array in C. I'm somewhat on the fence on how to approach the problem. I've looked through many forums and related topic but I still can't grasp it. If someone could show me or break it down as simple as possible. That would be greatly appreciated. By the way, the contents of the .csv file is like this. The array should consist of just the alphabet and the number. I was thinking about using a 2-D array. Is that an appropriate solution?

A,1
B,2
C,3
....
1
  • Yes it's somewhat a duplicate question, and i did take a look at the forum prior to making this one. I still don't get it. Commented Jan 23, 2017 at 3:20

2 Answers 2

5

Start by defining your data structure:

struct my_record {
    char name;
    int value;
};

Then you can read like this:

FILE* my_file = fopen(...);
struct my_record records[100];
size_t count = 0;
for (; count < sizeof(records)/sizeof(records[0]); ++count)
{
    int got = fscanf(my_file, "%c,%d", &records[count].name, &records[count].value);
    if (got != 2) break; // wrong number of tokens - maybe end of file
}
fclose(my_file);

Now you have a 1D array of structs, one for each row.

Sign up to request clarification or add additional context in comments.

4 Comments

What to do if number of entries in a row varies?
@Plutoniumsmuggler: Then you can store a std::vector<my_record> for each line.
C does not have std::vectors. I meant c not C++ (as the question says)
@Plutoniumsmuggler: Sorry I forgot when replying to your comment. In C you can use whatever dynamic array class you have at hand. If you have none, you might try this one: developer.gnome.org/glib/stable/glib-Arrays.html
2

You can just create an array of structs, as the other answer described.

Once you have the struct definition:

typedef struct {
    char letter;
    int number;
} record_t;

Then you can create an array of structs like this:

record_t records[26]; /* 26 letters in alphabet, can be anything you want */

Using a 2D array would be unnecessary, as wrapping the letter and number in a struct would be easier to handle.

In terms of reading your file, you can just read with fscanf() until 2 values are not found.

Here is some basic code you can use:

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

#define NUMLETTERS 26

typedef struct {
    char letter;
    int number;
} record_t;

int main(void) {
    FILE *fp;
    record_t records[NUMLETTERS];
    size_t count = 0;

    fp = fopen("letters.csv", "r");
    if (fp == NULL) {
        fprintf(stderr, "Error reading file\n");
        return 1;
    }

    while (fscanf(fp, " %c,%d", &records[count].letter, &records[count].number) == 2) {
        count++;
    }

    for (size_t i = 0; i < count; i++) {
        printf("%c,%d\n", records[i].letter, records[i].number);
    }

    fclose(fp);

    return 0;
} 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.