0

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.

userMilka
  • 37
  • 1
  • 7
  • There are various SO questions on how to do this. – Ami Tavory Oct 24 '15 at 18:11
  • 1
    Well [IETF RFC 4180](https://tools.ietf.org/html/rfc4180) is the common definition for [csv format](https://en.wikipedia.org/wiki/Comma-separated_values). But in this definition, both record separators (usually `\r\n` and field separators (usually `,` but often `;`) can be present in fields **provided those are enclosed in quote (`"`)**. Following this definition, `strtok` is of no use... More seriously, give description of your file and specify whether for example fields can contain separators or not. If you really need to support full RFC 4180 considere using lex+yacc instead of `strtok`. – Serge Ballesta Oct 24 '15 at 18:12
  • You could simplify your code dramatically by using C++11 and the regex class. Once you know how the columns are delimited in a csv file, it becomes easy to define a regex pattern and use the regex_search function to read column data. – dspfnder Oct 24 '15 at 18:17
  • Don't use `strtok`, as it modifies the C-style string. Convert your text to `std::string` and use one of the many `find` methods. – Thomas Matthews Oct 24 '15 at 18:49
  • A better idea is to use `std::getline` into a `std::string` and use `std::istringstream` for parsing, such as the `operator>>`. – Thomas Matthews Oct 24 '15 at 18:50
  • would any of you be so kind and give me maybe some example of how can be this done. I dont necessarily need to use strtok I was looking how to solve my problem online and applied it. I know there are many ways how this can be done but I do not know how to apply them .. Thank you – userMilka Oct 24 '15 at 18:59

0 Answers0