从.csv读取数据并在c ++中读取数组

时间:2012-03-11 00:38:20

标签: c++

我在这个网站和谷歌上也进行了一些搜索。但我无法理解我看到的很多代码,我希望从这里找到更直接的帮助。我只有2个学期的c ++,我有一个像我老板那样的副项目。

他为调用日志生成一个csv文件,我希望能够从日志中检索某些行,并能够计算和显示数据。

我不确定我需要问的具体问题但是我的代码是我试图开始获取数据但遇到问题(由于缺乏时间和经验,我的编程知识相当有限)。

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int main()
{
//opens the csv file.
ifstream gwfile;
gwfile.open("log.csv");

if(!gwfile) { // file couldn't be opened
  cout << "FAILED: file could not be opened" << endl << "Press enter to close.";
  cin.get();
  return 0;
}else
    cout << "SUCCESSFULLY opened file!\n";

cout << "-------------------------------------------------------------------------------\n\n\n";

long int SIZE = 0;
char data[SIZE];
cout << "This is data SIZE:" << data[SIZE] << endl;

//in the csv im trying to only read lines that start with the voice as those are only valid data we need.
//also i would like to display the headings in teh very first line
while( !gwfile.eof() ){
    //This is where im trying to only accept the lines starting with "Voice"
    //if(data[SIZE] == "Voice"){
        for( int i=0; i!=","; i++){
            cout << "This is i: " << i << endl; //testing purposes.
        }
    //}
//        getline(gwfile, data, '');
//        cout << data[0];
}
return 0;
}

1 个答案:

答案 0 :(得分:2)

让我们从显而易见的

开始
long int SIZE = 0;
char data[SIZE];
cout << "This is data SIZE:" << data[SIZE] << endl;

您正在创建一个大小为0的数组,然后到达其第一个成员:data [0]。这不行。为您的数组提供足够大的大小来处理您想要处理的数据,或者使用动态可调整大小的容器(例如std :: vector)来处理它。