最近在准备信息与未来的考试,看到模拟题上写说要文件输入输出,相信不止我一个不知道,下面就给大家科普一下~
二. 注意事项:
- 每题采用黑箱测试,输入输出均为文件,不通过文件输入或输出数据一律零分。
文件输入输出
首先让我们显比较一下这和普通的cin,cout有什么区别。
| 标准 | 文件 |
|---|
| 文件头为iostream | 文件头为fstream |
| 使用cin,cout | 使用fin,fout |
| 用>>,<< | 用<<,>> |
| ··· | 用完之后要close |
文件输入
#incldue<ifstream>
using namespace std;
int main(){
int n = 10;
ifstream fin("文件名.txt");
while(fin.good()){
fin >> n;
}
fin.close();
return 0;
}
文件输出
#include<iostream>
#include<fstream>
using namespace std;
int main(){
int n = 100;
ofstream fout;
fout.open("文件名.txt");
fout << n << endl;
fout.close();
return 0;
}