Hi there,
I'm trying to load a CSV file into a single dimentional array. I can output the contents of the CSV file, but in trying to copy it into an array I'm having a bit of trouble.
Here is my existing code, which I realise is probably pretty bad but I'm teaching myself:
#include <stdio.h>
#include <string.h>
#define MAX_LINE_LENGTH 1024
#define MAX_CSV_ELEMENTS 1000000
int main(int argc, char *argv[])
{
char line[MAX_LINE_LENGTH] = {0};
int varCount = 0;
char CSVArray[MAX_CSV_ELEMENTS] = {0};
FILE *csvFile = fopen("data.csv", "r");
if (csvFile)
{
char *token = 0;
while (fgets(line, MAX_LINE_LENGTH, csvFile))
{
token = strtok(&line[0], ",");
while (token)
{
varCount++;
CSVArray[varCount] = *token; //This is where it all goes wrong
token = strtok(NULL, ",");
}
}
fclose(csvFile);
}
return 0;
}
Is there a better way I should be doing this? Thanks in advance!