[問題] txt檔案讀取一行,放入到陣列並顯示

看板C_and_CPP (C/C++)作者 (Jay)時間6年前 (2019/09/12 19:17), 6年前編輯推噓2(202)
留言4則, 2人參與, 6年前最新討論串1/2 (看更多)
開發平台(Platform): (Ex: Win10, Linux, ...) Windows 10 編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出) vs2019 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 要把一個文字檔案,程式讀取每一行之後,透過寫入到陣列 然後呈現 餵入的資料(Input): 這是我的顯示,右邊是我設定的每一行的陣列 https://imgur.com/mPT1Bcc
預期的正確結果(Expected Output): 這是直接讀取txt檔案的內容,沒有空一行沒有關係 最重要的是能一行對應一個array https://imgur.com/NEVmEZj
錯誤結果(Wrong Output): 而我直接寫入到陣列之後,呈現的內容: https://imgur.com/YI4YaLO
這邊我只有先測試讀取到data_array[0][0]~data_array[2][3]的內容 程式碼(Code):(請善用置底文網頁, 記得排版,禁止使用圖檔) Sleep(5000); //5秒開始,目前回傳以密碼原則12條為主:檢查全部內容 string data_array[5][4]; ifstream Read_PwdRevisTxt; //要讀取的修改檔-PwdRevis Read_PwdRevisTxt.open("computer01.txt"); for(int i=0;i<5;i++) { for (int j = 0; j < 4; j++) { Read_PwdRevisTxt >> data_array[i][j]; } } Read_PwdRevisTxt.close(); //讀取完畢要關閉 cout << data_array[0][0] << endl; cout << data_array[0][1] << endl; cout << data_array[0][2] << endl; cout << data_array[0][3] << endl; cout << data_array[1][0] << endl; cout << data_array[1][1] << endl; cout << data_array[1][2] << endl; cout << data_array[1][3] << endl; cout << data_array[2][0] << endl; cout << data_array[2][1] << endl; cout << data_array[2][2] << endl; cout << data_array[2][3] << endl; 補充說明(Supplement): 我找到的問題,他好像只要有空白、tab,就會直接寫入陣列內@@ 我要的是一行對應一個array這樣 有大大可以告訴我怎麼解決這問題嗎? -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 218.161.102.123 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1568287041.A.636.html ※ 編輯: jayzhuang (218.161.102.123 臺灣), 09/12/2019 19:18:03

09/12 19:57, 6年前 , 1F
透過 getline() ?用法可google
09/12 19:57, 1F

09/12 21:14, 6年前 , 2F
宣告vector<string> data_array 用Readline(cin,data_array[
09/12 21:14, 2F

09/12 21:14, 6年前 , 3F
i]) 去取每一行
09/12 21:14, 3F

09/12 22:55, 6年前 , 4F
呃 我打錯api 是getline... 拍謝 恍神
09/12 22:55, 4F
謝謝各位大大,我再改看看 依照g大的方式,是可行! 不過他連換行也加入到array裡面了@@..... ※ 編輯: jayzhuang (218.161.102.123 臺灣), 09/16/2019 10:50:38
文章代碼(AID): #1TUYb1Os (C_and_CPP)
文章代碼(AID): #1TUYb1Os (C_and_CPP)