The daily ramblings …

C++ : I/O Streams – Output Formatting

Posted in C++ General by akamal on February 12, 2007

Creating a clean formatted data output is a common programming requirement. Take note however that output formatting requires the use of iomanip.h file, so don’t forget to include it. Anyway, listed below is a collection of tricks to use in C++.

Field Width
To set a uniform space between data, simply use the command setw(width).

1#include <iostream>
2#include <iomanip>
3using namespace std;
4
5int main()
6{
7	int val_int = 10;
8
9	//print val_int with no formatting
10	cout << val_int << endl;
11	//print val_int, padded on left with spaces to total 5 chars
12	cout << setw(5) << val_int << endl;
13	//print val_int, padded on right with spaces to total 5 chars
14	cout << setw(-5) << val_int << endl;
15	return 1;
16}

Filling blank spaces with values
Sometimes, you just don’t want to leave empty spaces in between formatted data, what you can do is replace it by using setfill(‘xxx’).

1#include <iostream>
2#include <iomanip>
3using namespace std;
4
5int main()
6{
7	int val_int[10] = {10,20,30,40,50};
8	//print val_int, padded on right with spaces to total 5 chars
9	for (int i=0;i<max;i++)
10	{
11		//this will replace blank space in between data with '-'
12		cout << setfill('-') << setw(5) << val_int[i] << endl;
13	}
14	return 1;
15}

Controlling precision
If you have to output a set of data with varying floating point length, use precision syntax.

1#include <iostream>
2#include <iomanip>
3using namespace std;
4
5int main()
6{
7	double val = 1000 / 33;
8	cout << setprecision(2) << val;
9	return 1;
10}

Number bases (hex,oct,bin,dec)
C++ also provides an easy conversion-on-the-fly method to convert number value to different bases. Look at the code below. Remember by default, any numerical variable defined is in base 10 by default.

1#include <iostream>
2#include <iomanip>
3using namespace std;
4
5int main()
6{
7	int val;
8	cin >> hex >> val;
9	cout << "You have entered " << val << ", this value is in decimal" << endl;
10	cout << val << " in base 8 is " << oct << val << endl;
11	cout << val << " in base 16 is " << hex << val << endl;
12	return 1;
13}

I have also included a sample code that reads/write data based on the examples above. Feel free to experiment with it. I trust that you’re smart enough to understand the codes.

1#include <iostream>
2#include <fstream>
3#include <iomanip>
4
5int row = 10;
6int col = 15;
7int width = 3;
8
9void write(const char *filename)
10{
11	std::ofstream file(filename);
12	for (int i = 0; i < row; i++)
13	{
14		for (int j = 1; j < col; j++)
15		{
16			file << std::hex << std::setw(width) << j;
17		}
18		file << 'n';
19	}
20}
21
22void read(const char *filename)
23{
24	std::ifstream file(filename);
25	int value;
26	for (int i = 0; i < row; i++)
27	{
28		for (int j = 1; j < col; j++)
29		{
30			file >> std::hex >> value;
31			std::cout << std::setw(width) << std::hex << value;
32		}
33		std::cout << 'n';
34	}
35}
36
37int main()
38{
39	char filename[] = "file.txt";
40	write(filename);
41	read(filename);
42	int t;
43	std::cin >> t;
44	return 1;
45}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: