Re: [問題] 將csv黨案讀入二維陣列中
※ 引述《pipited07 (天啊完全不知道取什麼)》之銘言:
: 小弟是c++新手希望別嫌問題太簡單@@
: 開發平台(Platform): visual studio 2013 c++
: 資料: excel檔案轉存的csv檔案
: 問題(Question):如果我有一筆確定行列的二維資料要怎麼寫能讓他存進陣列
: 好讓我能直接輸出呢?
確定行列是二維資料,就直接寫死讀法...
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char const *argv[]) {
string filename = "data.csv";
ifstream input(filename.c_str());
string line;
while(!input.eof()) {
// 讀一整行
get_line(input, line);
if(!input.eof()) {
// 找到逗號的位置
int comma_pos = line.find(',');
// 用string的函式抓出逗號分隔的sub string
string x = line.substr(0, comma_pos);
string y = line.substr(comma_pos+1);
// x跟y就是那一行的數值了
cout << x << " : " << y << endl;
}
}
input.close();
return 0;
}
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 60.244.41.5
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1441852316.A.7A3.html
※ 編輯: yichen (60.244.41.5), 09/10/2015 10:57:26
推
09/11 01:18, , 1F
09/11 01:18, 1F
討論串 (同標題文章)
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章