讀取文本文件並將文本文件中的令牌放入2-D矩陣中時出現問題。讀取文本文件並將令牌放入2D矩陣中的問題
這裏是我的代碼:
std::vector< vector <std::string> > my_matrix(10, vector <std::string>(10));
ifstream myReadFile;
myReadFile.open("class_data.txt", ios_base::in);
char output[100];
if (myReadFile.is_open()) {
if (!myReadFile.eof()) {
myReadFile >> output;
char* token = NULL;
char* context = NULL;
char delims[] = " ,\t\n";
token = strtok_s(output, delims, &context);
if (token != NULL)
{
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
my_matrix[i][j] = *token;
cout << "token = " << token << endl;
cout << "matrix = " << my_matrix[i][j] << endl;
token = strtok_s(NULL, delims, &context);
}
}
}
}
}
std::cout.width(3); std::cout << left << "ID";
std::cout.width(9); std::cout << left << "Project1";
std::cout.width(9); std::cout << left << "Project2";
std::cout.width(9); std::cout << left << "Project3";
std::cout.width(9); std::cout << left << "Project4";
std::cout.width(9); std::cout << left << "Project5";
std::cout.width(9); std::cout << left << "Project6";
std::cout.width(9); std::cout << left << "Project7";
std::cout.width(9); std::cout << left << "Midterm";
std::cout.width(9); std::cout << left << "Final" << std::endl;
std::cout << "-------------------------------------------------------------------------------" << std::endl;
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
cout.width(10); std::cout << my_matrix[i][j];
}
std::cout << " " << std::endl;
}
我沒有錯誤,它編譯,但該程序還當我想顯示2-d矩陣,它不會顯示出來。它對整個矩陣沒有任何價值。
任何人都可以幫助我,告訴我什麼是問題?這是否與我爲循環2做的方式一樣,還是我閱讀文本文件的方式?如果發現問題,任何人都可以提出解決方案。
P.S對不起,如果我沒有最好的專業代碼,但我是C++初學者,我沒有參加任何大學水平的編程課程。
你能否展示輸入數據的外觀? –
classdata.txt contains this text:A00529154 76 79 85 91 75 80 90 56 58 A00656624 79 85 0 86 86 76 51 89 92 A02507691 47 94 92 49 77 72 25 25 95 A00612352 41 82 90 58 87 0 50 98 80 A04012435 91 50 78 68 70 60 42 74 85 A00654400 47 94 89 75 80 76 0 71 83 A00577109 44 88 84 86 89 88 99 100 90 A00580920 41 82 80 90 89 97 93 84 86 A04028610 90 90 90 90 90 90 90 90 90 A04063494 90 90 90 90 70 90 90 90 90 –