//把二维数组内容写入到文件中
int a[10][20];
..... //二维数组赋值
CFile f;
f.open("data.txt", CFile::modewrite|CFile::modecreate);
if( !f ) return; //打开文件失败
int row=10, col=20;
f.write(&row, sizeof(row) ); //写入行数
f.write(&col, sizeof(col) ); //写入列数
for(int i=0; i<row; i++)
f.write( a[i], sizeof(int) * col ); //每次把一行的数据写入文件中
f.close();
//从文件中读入数据到二维数组
int b[10][20];
CFile f;
f.open("data.txt", CFile::modeRead| CFile::shareDenyWrite);
if( !f ) return;
int row,col;
f.read(&row, sizeof(row) );
r.read(&col, sizeof(col) );
//比较行、列数是否相同
if( row != 10 || col != 20 )
{ AfxMessageBox("行列数不符");
return;
}
//读出数据
for(int i=0; i<row; i++)
f.read( b[i], sizeof(int) * col ); //每次把一行的数据读出
f.close();
温馨提示:答案为网友推荐,仅供参考