本篇非常肤浅,作个人之用。
各个类间通过继承连接。 用法直接看代码. 步骤有三:
1. 表示从文件读取数据 , 方式是in
2. 通过某种 数据结构 来存储,再用 算法 处理
3. 表示 从数据结构中输出数据到某个文件, 方式是out
#include <fstream>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
int a[8];
ifstream fin ("input.txt", ios::in); //表示从文件读取数据 , 方式是只读
if(!fin)
{
cerr << "erro" << endl;
return 0;
}
//通过某种数据结构来存储,再用算法处理
ofstream fout("output.txt", ios::out ); //表示 从数据结构中输出数据到某个文件, 方式是out
if(!fout)
{
cerr << "erro" << endl;
return 0;
}
for(int i = 0; i != 8; ++i)
fin >> a[i];
sort(a, a+8);
for(int i = 0; i < 8; ++i)
fout << a[i] << ' ';
fout << endl;
int b[10];
for(int i = 0; i != 10; ++i)
fin >> b[i];
sort(b, b+10);
for(int i = 0; i < 10; ++i)
fout << b[i] << ' ';
fout << endl;
fin.close();
fout.close();
}
使用完必须 close,不然很危险
打开文件的方式
ios::in 为输入(读)而打开文件
ios::out 为输出(写)而打开文件
ios::ate 初始位置:文件尾
ios::app 所有输出附加在文件末尾
ios::trunc 如果文件已存在则先删除该文件
ios::binary 二进制方式
可以用 | 连接
ofstream out;
out.open("Hello.txt", ios::in|ios::out|ios::binary)
需要特别注意的是在 为 windows 中,可以特别的用二进制打开。 但是在 linux 中, 只有二进制打开文本这一种方式。