作者evanslee (321)
看板C_and_CPP
标题Re: [问题] 读档 求救!!!
时间Mon Mar 16 22:52:20 2009
※ 引述《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)