生活随笔
收集整理的這篇文章主要介紹了
从文件中读取数据,排序之后输出到另一个文件中
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
文件中有一組數(shù)據(jù),要求排序后輸出到另一個文件中去
主要有兩個知識點: 排序、文件操作
C++/C代碼如下:
[cpp]?view plaincopy
#include<iostream>?? #include<fstream>?? #include<vector>?? using?namespace?std;?? void?Order(vector<int>?&data)? 采用了引用 {?? ????int?count=data.size();?? ????int?i,j;?? ????int?temp; ? ????for?(i=0;i<count-1;i++)?? ????{?? ????????for?(j=0;j<count-1-i;j++)?? ????????{?? ????????????if?(data[j]>data[j+1])?? ????????????{?? ????????????????temp=data[j];?? ????????????????data[j]=data[j+1];?? ????????????????data[j+1]=temp;?? ????????????}?? ????????}?? ????}?? }?? ?? int?main()?? {?? ????int?i = 0;?? ????vector<int> data;?? ????ifstream?in("wo1.txt");?? ????int?temp = 0;?? ????if?(!in)?? ????{?? ????????cout<<"打開文件失敗"<<endl;?? ????????exit(1);?? ????}?? ????while?(!in.eof())?? ????{?? ????????in>>temp;?? ????????data.push_back(temp);?? ????}?? ????in.close();?? ????Order(data);?? ????ofstream?out("wo2.txt");?? ????if?(!out)?? ????{?? ????????cout<<"file?open?error";?? ????????exit(1);?? ????}?? ????for?(i=0;i<data.size();i++)?? ????{?? ????????out<<data[i]<<"??";?? ????}?? ????out.close();?? ????cout<<endl;?? ????return?0;?? } ?
總結(jié)
以上是生活随笔為你收集整理的从文件中读取数据,排序之后输出到另一个文件中的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。