这个主要复习一下文件输入输出流~~
//文件中有一组整数,要求排序后输出到另一个文件中 #include <iostream> #include <fstream> //文件输入输出流 #include <vector> using namespace std; int main() { ifstream InFile("in.txt"); if(InFile.good()) { cout<<"open file success!"<<endl; } ofstream OutFile("out.txt"); vector<int> num; int temp; while(!InFile.eof()) { InFile>>temp; num.push_back(temp); } vector<int>::iterator i; cout<<"before sort..."<<endl; for(i=num.begin();i!=num.end();++i) cout<<*i<<" "; cout<<endl; int k; for(k=0;k<num.size();k++) for(int j=0;j<num.size()-k-1;j++) if (num[j]>num[j+1]) { temp=num[j]; num[j]=num[j+1]; num[j+1]=temp; } cout<<"after sort.."<<endl; for(i=num.begin();i!=num.end();++i) { cout<<*i<<" "; OutFile<<*i<<" "; } cout<<endl; InFile.close(); OutFile.close(); return 0; }
时间: 2024-12-20 10:50:38