我花了几个小时试图将csv(excel)文件读入矢量。我曾尝试使用strtok,但它总是返回整个电子表格(所有内容,甚至在重新调整大小后都无法适应控制台窗口大小。所以我很难理解数据发生了什么)。 excel电子表格由26列和大量行组成。它们在线更新。但我只需要保存4列。我想使用向量来保存每个列数据。我感谢任何帮助。这是我的代码
#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
这是电子表格格式,共有26列。我需要保存QuatW QuatX QuatY QuatZ。