2013-12-15 18 views
0

我正在嘗試將一個.csv文件讀入C++算法中,並且我試圖將每列存儲在內置於結構中的不同字符串數組中。工作正常,直到棧溢出錯誤發生,任何人都可以知道錯誤在哪裏? 的#include 的#include 的#include 的#include當試圖將.csv數據複製到C++中的結構時,堆棧溢出錯誤

using namespace std; 
    struct burgerking //structure containing different strings for each column in .csv file 
    { 
string longitude[7000]; 
string latitude[7000]; 
string location[7000]; 
string state[7000]; 
string address[7000]; 
    }; 

    void main() { 
    burgerking burger; 
    string line; 
ifstream myfile; 
myfile.open("burgerking.csv"); //opening the csv file 
if(myfile.good()) 
    cout<<"File is Good to be opened"<<endl; 
int l=0; 
int n=1; 
int e=2; 
int ss=3; 
int j=0; 
int b=0; 
int kk=0; 
int ll=0; 
for(int i=0;i<40;i++) 
{ 
    getline(myfile,line,','); 
    if(i==0) 
    { 
     burger.longitude[j]=line; 
     j++; 
     l=l+7; 
    } 
    if(i==l) 
    { 
     burger.longitude[j]=line.substr(16,line.length()); 
     j++; 
     l=l+7; 
    } 
    if(i==n) 
    { 
     burger.latitude[b]=line; 
     n=n+7; 
     b++; 
    } 
    if(e==i) 
    { 
     burger.location[kk]=line; 
     kk=kk+1; 
     e=e+7; 
    } 
    if(ss==i) 
    { 
     burger.state[ll]=line; 
     ss=ss+7; 
     ll++; 
    } 
} 

//myfile.close(); 
//myfile.open("burgerking.csv"); 
//for(int c=0;c<20;c++) 
//{ 
// getline(myfile,line,','); 
// if(c==3) 
// { 
//  burger.address[0]=line; 
// } 
//} 

for(int k=0;k<1;k++)// loop just to check the program output 
{ 
    cout<<burger.state[k]<<endl; //just to check the output 

} 



myfile.close(); 
system("PAUSE"); 

}

+1

我猜35,000個字符串比棧可以處理更多。 –

+0

那我該怎麼辦呢? – Murtaza

回答

0

您所創建的堆棧上有非常大的結構。你的「漢堡」變量至少需要140KB的堆棧空間,這太多了。我建議你從堆中分配它,即

burgerking* burger; 
burger = new burgerking(); 
+0

使用'malloc'來分配一個包含需要運行默認構造函數的字符串的結構是一個可靠的配方。 –

+0

@RetiredNinja是的,我的錯,很快就會編輯它。我頭腦中的C太多了。 –

+0

如果不是在結構中創建35000個陣列,我會創建7000個結構,如 burgerking burger [7000]; – Murtaza