2012-03-11 45 views
-1

我在本網站和google上搜索了一些內容。但我無法理解我看到的很多代碼,我希望能從這裏找到更多的直接幫助。我只有2個學期的C + +,我有一個副項目編號喜歡爲我的老闆做。從.csv讀取數據並存入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; 
} 
確切的問題
+0

是否有一個特別的原因,你想在C++中做到這一點? – 2012-03-11 00:40:03

+0

我只知道C++,對我來說這似乎是一個很棒的任務。 – Nickoli 2012-03-11 01:14:46

+0

任何其他幫助? – Nickoli 2012-03-12 20:08:32

回答

2

讓我們從明顯

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

您正在創建大小爲0的數組開始,然後達到其第一構件:數據[0]。這是行不通的。給你的數組一個足夠大的大小來處理你想要處理的數據,或者使用一個動態調整大小的容器(如std :: vector)來處理它。

+0

更不用說'SIZE'是一個變量,我們在C++中沒有可變大小的數組(儘管我們在GNU擴展中做) – Shahbaz 2012-03-11 01:09:46

+0

我以爲我可以嘗試將它們放入數組,所以我可以添加某些數據。但我也使用getline(gwfile,字符串數據,','),我能夠捕捉整行,但我不能確定如何捕捉額外的行。 – Nickoli 2012-03-11 01:22:09

+0

另外我不知道載體在這一點上。 – Nickoli 2012-03-11 01:29:09

相關問題