我是新來使用C + +和Microsoft Visual Studio,我試圖將一個數據文件(由浮點數組成的500列2列)轉換爲數組,然後我試圖在屏幕上輸出數組。每當我嘗試運行它時,它都會出現「file.exe中0x001BC864未處理的異常:0xC0000005:訪問衝突寫入位置0x00320A20」。
我發現這個視頻,並試圖改編該代碼https://www.youtube.com/watch?v=4nz6rPzVm70 任何幫助,將不勝感激。在C++教程程序崩潰(閱讀矩陣)
#include<stdio.h>
#include<string>
#include<iostream> //
#include<fstream> //
#include<array> //
#include<iomanip> //
#include<sstream>//
#include<conio.h>
using namespace std;
int rowA = 0; //
int colA = 0;
int main()
{
string lineA;
int x;
float arrayA[2][500] = { { 0 } };
ifstream myfile("C:/results.dat");
if (myfile.fail()) {
cerr << "File you are trying to access cannot be found or opened";
exit(1);
}
while (myfile.good()) {
while (getline(myfile, lineA)) {
istringstream streamA(lineA);
colA = 0;
while (streamA >> x) {
arrayA[rowA][colA] = x;
colA++; }
rowA++; }
}
cout << "# of Rows --->" << rowA << endl;
cout << "# of Columns --->" << colA << endl;
cout << " " << endl;
for (int i = 0; i < rowA; i++) {
for (int j = 0; j < colA; j++) {
cout << left << setw(6) << arrayA[i][j] << " ";
}
cout << endl;
}
return 0;
_getch();
}
如果你有500行兩個浮點數,那麼'float arrayA [2] [500]'和'arrayA [rowA] [colA]'不能很好地結合在一起。如果你有兩條500浮標,他們會。 – Wintermute 2015-04-02 15:04:16
好的,謝謝你,你有什麼我可以做的建議嗎? – Student201 2015-04-02 15:06:29
這個問題不是特定於Visual Studio的。這是一個通用的C++問題。你在使用C++數組時遇到了問題。掌握編程的第一步是能夠確定問題的根源。 – BitTickler 2015-04-02 15:09:24