C++文件读取:常见的ACM输入格式读取
文件流头文件fstream
在C++中,对文件的操作是通过stream的子类fstream(file stream)来实现的,所以,要用这种方式操作文件,就必须加入头文件fstream.h。下面就把此类的文件操作过程一一道来。
代码演示
#include <iostream>
#include <fstream>
using namespace std;
int main() {
int arr[100][100];
ifstream ifs("in.txt");
int r,c;
ifs >> r >> c;
cout << r << " " << c << endl;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
ifs >> arr[i][j];
cout << arr[i][j] << " ";
}
cout << endl;
}
return 0;
}
输出:
2 4
1 2 3 4
5 6 7 8