ait-survey

color_sort_bin.cpp

Apr 5th, 2015
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <fstream>
  4. #include <iomanip>
  5. #include <sstream>
  6. #include <cstdlib>
  7. using namespace std;
  8.  
  9. int main(int argc,char *argv[])
  10. {
  11.  
  12.   streampos begin,end;
  13.   //char * memblock;
  14.   //int * memblock;
  15.     int size;
  16.     int number;
  17.  
  18.     int *array;
  19.  
  20.     if(argc!=3)
  21.     {
  22.         cout << "argument error" << endl;
  23.     return 1;
  24.   }
  25.    
  26.   //ifstream file ("example.bin", ios::in|ios::binary|ios::ate);
  27.   //ifstream file (argv[1], ios::in|ios::binary|ios::ate);//ifstream:input file class
  28.   ifstream file (argv[1], ios::in|ios::binary);
  29.  
  30.   //    ofstream: Stream class to write on files
  31.   //  ifstream: Stream class to read from files
  32.   //  fstream: Stream class to both read and write from/to files.
  33.  
  34.     number=atoi(argv[2]);
  35.  
  36.  
  37.   if (file.is_open())
  38.   {
  39.        
  40.     begin = file.tellg(); //size = file size, tellg-Get position in input sequence
  41.        
  42.         file.seekg(0,ios::end);
  43.        
  44.         end = file.tellg();
  45.        
  46.         size = end - begin;
  47.        
  48.         cout << "size=" << size <<endl;
  49.  
  50.     //memblock = new char [size];
  51.     //memblock = new char [size];
  52.  
  53.         //cout << "memblock=" << memblock <<endl;
  54.  
  55.     //file.seekg (0, ios::beg); //ios::beg-offset counted from the beginning of the stream
  56.                                                             //Set position in input sequence
  57.     //file.read (memblock, size);//read block of data
  58.        
  59.         int pos=file.tellg();
  60.  
  61.         array = new int[size];
  62.        
  63.         file.seekg(0,ios::beg);
  64.  
  65.         file.read((char *)array,size);
  66.  
  67.     sort(array, array + number);
  68.  
  69.     for (int i = 0; i < number; ++i)
  70.         {
  71.             cout << array[i] << endl;
  72.         }
  73.  
  74.        
  75.     file.close();
  76.    
  77.     cout << "the entire file content is in memory\n";
  78.  
  79.     delete[] array;
  80.     }
  81.  
  82.     else cout << "Unable to open file";
  83.  
  84.     return 0;
  85.    
  86. }
Advertisement
Add Comment
Please, Sign In to add comment