Open file

Please help me write code for this task: open a .txt file ( with more than 100 characters ). Then sort the FIRST 100 characters and show the result by standard output
Thank you
Tutorial for input/output text-files - http://www.cplusplus.com/doc/tutorial/files/

I'm pretty sure you can find most if not all of what you need there + a bit of Googling.
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
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;



int main()
{
	char buffer[100];
	fstream myfile;

	// read text as binary data
	myfile.open("textfile.txt", ios_base::binary |ios_base::in);

	if (myfile)
	{		
		// opening was success
		if (myfile.read(buffer, 100))
		{
                        // reading was success
			myfile.close();
			cout << "unsorted numbers: ";
			for( int i = 0; i < 100; i++)cout << buffer[i] << " ";

			cout << endl;
			// sort number
			sort(buffer, buffer + 100);
			cout << "sorted numbers: ";
			for( int i = 0; i < 100; i++)cout << buffer[i] << " ";
		}
		else
		{
			cout << "It was error under reading!";
		}
	}


	return 0;
}
Thank you. How about to display full content of the txt file?
I did it. Thank for help
Topic archived. No new replies allowed.