读取文件中数据,并赋值给二维数组

void main()
{
 int row, col;
 cout << "Please enter the number for row and column: " << endl;
 cin >> row >>col;

 //为二维数组开辟空间
 int **Result = new int*[row];
 for (int i = 0;  i < row;  i++)
 {
  Result[i] = new int[col];
 }

 ifstream fin("./data.txt");
 for (int i = 0; i != row; ++i) {
  for (int j = 0; j != col; ++j) {
   fin >> Result[i][j];
  }
 }
 fin.close();

 for (int w=0;w<row;w++)
 {
  for (int r=0;r<col;r++)
  {
   if(r<=w)
    cout<<0<<",";
   else
    cout<<Result[w][r]<<",";
  }
  cout<<endl;
 }

 //释放二维数组占用的空间
 for (int m = 0; m < row; m++)
 {
  delete[] Result[m];
 }
 delete[] Result;
 Result = NULL;
 //system("pause");
 return;
}

posted on 2011-10-30 09:41  Livid  阅读(526)  评论(0编辑  收藏  举报

导航