Re: [問題] 讀檔 求救!!!
※ 引述《rich288719 (小熊)》之銘言:
: 假設我現再有一個2維的陣列 char str[4][2] ;
: 以下是記事本的內容
: 3
: 3
: 1 2 3
: 3 4 5
: 4 5 6
: 我該怎麼寫 才能將一個字元一個字元輸入到我的陣列
剛好我寫過類似的 這個可以輸入3*3 martrix 供參考
#include <sstream>
#include <fstream>
#include <string>
#include <iostream>
#include <iomanip>
using namespace std;
const int rowDim=3;
const int colDim=3;
int a[rowDim][colDim];
void readFile(int matrix[rowDim][colDim], int row, int col, string filename) {
string buf;
ifstream inFile;
inFile.open(filename.c_str());
int i=0;
while (getline(inFile, buf)) {
stringstream token(buf);
int j=0;
while(1) {
token >> matrix[i][j];
if (token.fail()) break;
j++;
}
i++;
}
}
int main(){
cout<<"請選擇矩陣"<<endl;
string A;
getline(cin,A);
readFile(a,rowDim,colDim,A);
cout<<"A"<<endl;
for(int i=0;i<rowDim;i++){
for(int j=0;j<colDim;j++) cout<<setw(5)<<*(a[i]+j);
cout<<endl;
return 0;
}
※ 編輯: evanslee 來自: 140.112.218.55 (03/16 22:54)
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章