Files
python/C++专题课程/文件输入与输出/readTwoDimensionArrayData.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

52 lines
908 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
#define MAX 101
int D[MAX][MAX];
static int n;
/**
* 功能:读取一个二维数据文件
* 作者:黄海
* 时间:2019-11-02
*/
void readTwoDimensionArrayData() {
//数据源文件
string file = "./TwoDimensionArrayDataData.in";
//fstream 流方法读数据
ifstream fin(file);
//读取数塔的层数
fin >> n;
//和cin一样的读取办法
for (int i = 1; i <= n; i++)
for (int j = 1; j <= i; j++)
fin >> D[i][j];
//关闭文件
fin.close();
fin.clear(ios::goodbit);
}
int main() {
//从数据文件中读取数据
readTwoDimensionArrayData();
//输出n
cout << "n=" << n << endl;
//输出二维数组
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
cout << D[i][j] << " ";
}
cout << endl;
}
cout << endl;
}