2014-05-04 118 views
-6

我正在製作一個程序,讀取有一堆銷售數字的文本文件,它工作正常,除了我繼續得到一個錯誤,指出:訪問衝突讀取位置0xCCCCCCCC。訪問衝突讀取位置0xCCCCCCCC

這裏是我的代碼的相關部分: 任何幫助將不勝感激。

void task1() 
{ 

// const int MAX_CHARS_PER_LINE = 512; 
// const int MAX_TOKENS_PER_LINE = 20; 
//const char* const DELIMITER = " "; 

string year; 
string line; 
double unitsold1[12],unitsold2[12],unitprice1[12],unitprice2[12]; 
string month[12]; 

string name="D:\\Sandesh Pyakurel\\Model_X_Sale_"; 
cout<<"\nPlease enter a year in the yyyy format: "; 
cin>>year; 

string result=name+year+".txt"; 

ifstream myfile; 
myfile.open(result.c_str()); 

double a,b,c,d; 
if(!myfile) 
{ 
    cout<<"Could not open file"<<endl; 

} 

if (myfile.is_open()) 
{ 

    int count =0; 
    while (myfile>>month[count] 
       >>unitsold1[count] 
       >>unitprice1[count] 
       >>unitsold2[count] 
       >>unitprice2[count] ) 
      { 
       count++; 
      } 

    myfile.close(); 
    for(int i=0;i<=count;i++) 
    { 
     cout<<month[i]<<" "<<unitsold1[i]<<" "<<unitprice1[i]<<" " << 
        unitsold2[i]<<" "<<unitprice2[i]<<" "<<endl; 

    } 




     /* 
     char myarray[5]; 

     for(int i=0;i<=5;i++) 
       { myfile.getline(myarray,100); 
        cout<<myarray[i]; 
       } 
     */ 
     /* 
      char buf[100]; 
      myfile.getline(buf,100); 


      int n=0; 
      const char* token[100]={}; 



      token[0]=strtok(buf,DELIMITER); 
      if(token[0]) 
      { 
       for (n=1;n<MAX_TOKENS_PER_LINE;n++) 
       { 
        token[n]=strtok(0,DELIMITER); 
        if(!token[n]) 
         break; 
       } 

      } 



      for(int i=0;i<n;i++) 
      { 

       stringstream ss(token[i]); 
       ss>>tmp>>a>>b>>c>>d; 
       cout<<ss<<endl; 
      }*/ 

} 

} 
+0

替換'我<= count'用'I

+2

參見例如http://stackoverflow.com/questions/15302364/c-access-violation-reading-location-0xcccccccc?rq=1 0xcccccccc模式通常用於填充內存,以便它可以被識別爲未初始化,用於調試構建。所以你使用了一個未初始化的指針。 –

+2

如果你已經完成了最少量的調試,你會發現問題發生在'i == 12'時。 –

回答

0

假定您在輸入文件中有十二個月,您正在讀取數組的末尾。替換:

for(int i=0;i<=count;i++) 
{ 

與:

for(int i=0; i < count; ++i) 
{