2014-04-03 63 views
0

讀取文本文件並將文本文件中的令牌放入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++初學者,我沒有參加任何大學水平的編程課程。

+0

你能否展示輸入數據的外觀? –

+0

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 –

回答

1

您已經有了一個10×10矩陣的strtok返回一個char *爲當前的道理,只是把它傳遞給的std :: string運算符=在矩陣是這樣的:

my_matrix[i][j] = token; 

您還需要重新思考循環中的邏輯。在內部for strtok調用之後,爲返回值添加一個檢查,如果它爲NULL,則從兩個循環中斷開。用if測試替換while循環。

編輯:

我看你改變了,而到如果,但是,2 for循環需要一點點變化了。我建議這樣的:

for (int i = 0; i < 10 && token; i++) { 
    for (int j = 0; j < 10 && token; j++) { 
     my_matrix[i][j] = token; // NOTE: not *token here 
     cout << "token = " << token << endl; 
     cout << "matrix = " << my_matrix[i][j] << endl; 
     token = strtok_s(NULL, delims, &context); 
    } 
} 

這個改變也使循環中的if測試成爲不必要的。

+0

謝謝你的回答,我會在今天晚些時候嘗試,我會在幾個小時內回覆你 –

+0

謝謝,現在一切正常。 –

+0

Hi @DNT。你以前幫過我,你能再幫我一次嗎,這次我有一個不同的問題。你可以看看http://stackoverflow.com/questions/23233993/how-to-declare-a-font-variable-properly或http://stackoverflow.com/questions/23189706/how-to-use-字體作爲一個變量,請你能告訴我如何正確地做到這一點? –

相關問題