看板 C_and_CPP 關於我們 聯絡資訊
Win10 GCC 小弟需要將一個txt檔的資料存成double型態的二維陣列 資料來源如附圖 https://imgur.com/a/bdjQbiR 是一個144列*3行的數據 這是我的code #include <iostream> #include <fstream> #include <string> #include <sstream> using namespace std; int main() { ifstream inFile("Point_After.txt"); const int xsize = 3; const int ysize = 144; double (*arr)[xsize] = new double[ysize][xsize]; string line; int y = 0; while(getline(inFile,line)) { istringstream ss(line); double num; int x = 0; while(ss >> num) { arr[y][x] = num; ++x; } ++y; } inFile.close(); for(int y = 0; y != ysize; ++y) { for(int x = 0; x != xsize; ++x) { cout << arr[y][x] << " "; } cout << endl; } delete []arr; return 0; } 這是我執行後的結果 https://imgur.com/a/KakhsWZ 想請問各位前輩為何無法成功 謝謝 主要問題我想出在字串轉成double無法成功 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.39.212.82 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1668969181.A.54E.html
chingying0: getline的delimiter有加'v' 執行結果如下 11/21 02:38
chingying0: https://imgur.com/a/ZkULZSE 11/21 02:39
ko27tye: 你沒處理每行資料內的空格吧 11/21 08:58
mihonisizumi: 因為你每行前面有 'v' 所以轉失敗 11/21 12:49
mihonisizumi: getline 不加 delim,然後在 while(ss >> num) 前 11/21 12:57
mihonisizumi: 面加上char c;ss>>c; 11/21 12:57
wulouise: 你的v跟空白沒處理吧 11/22 22:37