本文意在熟悉流对文件的操作
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
void Order(vector<int> &data)
{
for (unsigned int i = 0; i < data.size(); i++)
{
for (unsigned int j = i+1; j < data.size(); j++)
{
if (data.at(i) > data.at(j))
{
data.at(i) ^= data.at(j);
data.at(j) ^= data.at(i);
data.at(i) ^= data.at(j);
}
}
}
}
void printFunc(vector<int>& data)
{
for (unsigned int i = 0; i < data.size(); i++)
{
cout<<data.at(i)<<"\t";
}
cout<<endl;
}
void main( void )
{
vector<int>data;
ifstream in("data.txt");
if ( !in)
{
cout<<"file error!";
exit(1);
}
int temp;
while (!in.eof())
{
in>>temp;
data.push_back(temp);
}
in.close();
printFunc(data);
Order(data);
printFunc(data);
ofstream out("output.txt");
if ( !out)
{
cout<<"file error!";
exit(1);
}
for (unsigned int i = 0 ; i < data.size() ; i++)
out<<data[i]<<" ";
out.close();
}
上面一行是 data.txt 中的数据
下面一行是 从小到大排序后 输出到 output.txt 文件中的数据