Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <fstream>
- #include <iomanip>
- #include <sstream>
- #include <cstdlib>
- using namespace std;
- int main(int argc,char *argv[])
- {
- streampos begin,end;
- //char * memblock;
- //int * memblock;
- int size;
- int number;
- int *array;
- if(argc!=3)
- {
- cout << "argument error" << endl;
- return 1;
- }
- //ifstream file ("example.bin", ios::in|ios::binary|ios::ate);
- //ifstream file (argv[1], ios::in|ios::binary|ios::ate);//ifstream:input file class
- ifstream file (argv[1], ios::in|ios::binary);
- // ofstream: Stream class to write on files
- // ifstream: Stream class to read from files
- // fstream: Stream class to both read and write from/to files.
- number=atoi(argv[2]);
- if (file.is_open())
- {
- begin = file.tellg(); //size = file size, tellg-Get position in input sequence
- file.seekg(0,ios::end);
- end = file.tellg();
- size = end - begin;
- cout << "size=" << size <<endl;
- //memblock = new char [size];
- //memblock = new char [size];
- //cout << "memblock=" << memblock <<endl;
- //file.seekg (0, ios::beg); //ios::beg-offset counted from the beginning of the stream
- //Set position in input sequence
- //file.read (memblock, size);//read block of data
- int pos=file.tellg();
- array = new int[size];
- file.seekg(0,ios::beg);
- file.read((char *)array,size);
- sort(array, array + number);
- for (int i = 0; i < number; ++i)
- {
- cout << array[i] << endl;
- }
- file.close();
- cout << "the entire file content is in memory\n";
- delete[] array;
- }
- else cout << "Unable to open file";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment