I spent couple of hours trying to read a csv (excel) file into vectors. I ve tried to use strtok but it always returns the whole spreadsheet (everything and it even cannot fit the console window size after re-sizing it. So it is really hard for me understand what is going on with the data). excel spreadsheet consists of 26 columns and loads of rows. They are updated online. But I only need to save 4 columns. I would like to use a vector for saving each column data. I appreciate any help.This is my code
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int main ()
{
// Read the file ----------
FILE* fp = fopen("toto.csv", "rb");
if (fp == NULL) return 0;
fseek(fp, 0, SEEK_END);
long size = ftell(fp);
fseek(fp, 0, SEEK_SET);
char *pData = new char[size + 1];
fread(pData, sizeof(char), size, fp);
fclose(fp);
// Read the file ----------
// Parse the file content ----------
char* pch;
pch = strtok (pData, "|");
int iCpt = 1;
while (pch != NULL)
{
if (iCpt == 1 || iCpt == 2|| iCpt == 3)
{
printf ("%s\n", pch);
}
pch = strtok (NULL, "|");
iCpt++;
}
// Parse the file content ----------
system ("PAUSE");
return 0;
}
SensorId|TimeStamp (s)|QuatW|QuatX|QuatY|QuatZ
1 | 0 |-0.06|-0.05|-0.98|1.19
this is the spreadsheet format and there are 26 columns. I need to save QuatW QuatX QuatY QuatZ.