1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
#include<iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <direct.h>
#include <iomanip>
#define GetCurrentDir _getcwd
using namespace std;
enum {up,down};
std::ifstream::pos_type filesize(const char* filename)
{
std::ifstream in(filename, std::ifstream::ate | std::ifstream::binary);
return in.tellg();
}
template <typename S>
void save(const char * filename,S array)
{
FILE * f = fopen(filename, "wb");
int size= array.size()*sizeof(array[0]);
auto ap = &array[0];
fwrite(ap,size,1,f) ;
fclose(f);
}
template <typename L>
void load(const char * filename,L &array)
{
int s=filesize(filename)/sizeof(array[0]);
array.resize(s);
FILE * f = fopen(filename, "rb");
auto ap = &array[0];
fread(ap,array.size()*sizeof(array[0]),1,f);
fclose(f);
}
void dossort(string filename,int direction,string fileout="_out_.txt")
{
string cmd;
char CurrentPath[1024];
GetCurrentDir(CurrentPath, sizeof(CurrentPath));
string cd=CurrentPath;
if (direction==down)
cmd = "SORT/r " +cd + "\\"+ filename + " /o " + cd +"\\"+fileout ;
else
cmd = "SORT " + cd + "\\"+ filename + " /o " + cd +"\\"+fileout ;
system(cmd.c_str());
}
int main()
{
char * filein= (char *)"names2.txt";
char * fileout=(char *)"_out_.txt";
string s =
"Samir\nAmir\nManane\nHarry\nHaris\nHugo\nMickael\nKevin\nMoustapha\nKarl\nmaxime\nHugo\nHadi\nMalick\nAkim\nRhagiv\nEmeric\nGilles\nOswald\naxel";
cout<<"original file:"<<endl;
cout<<s<<endl<<endl;
save(filein,s);
string resultup,resultdown;
dossort(filein,up,fileout);
load(fileout,resultup);
dossort(filein,down,fileout);
load(fileout,resultdown);
cout<<"sorted file:"<<endl;
cout<<" up:"<<setw(15)<<" down:"<<endl;
istringstream RU( resultup );
istringstream RD( resultdown );
string tu,td;
while ( RU>>tu && RD>>td)
{
cout<<left<<setw(15)<<tu<<setw(10)<<td<<endl;
}
cout <<"Press return to delete the text file "<<filein<<" and "<<fileout<<endl;
cin.get();
if( remove( filein ) != 0 || remove( fileout ) != 0 )
cout<<"Error deleting files, please complete the task manually"<<endl;
else
{
cout<<endl;
cout<< "Files successfully deleted"<<endl;
}
cout <<" "<<endl;
cout <<"Press return to end . . ."<<endl;
cin.get();
}
|