The C++ manipulators are stream functions that modify the characteristics of input and output stream. It is used for formating the input and output stream by changing format flags and values for the stream.
The list of manipulator function is located in <iomanip> header file. You need to include this header to use the manipulator functions in your program.
The list of C++ standard manipulator functions is as follows.
endl
hex, oct, dec
setbase
setw
setfill
setprecision
ends
ws
flush
setiosflags
resetiosflags
The hex, dec, oct, ws, endl, ends, and flush are defined in the stream header file. The rest are defined in the iomanip header files.
endl
The endl introduce a new line or a line feed character. It is similar to C programming language “\n” character and C++ supports the old line feed.
Example:
cout << "This line use line feed" << endl;
cout << number1 << endl << number2 << endl;
You can use it anywhere and a new line character is added automatically.
setbase()
The setbase() manipulator is a manipulator that changes the base of a number to another base value. The C++ language supports following base values:
hex (Hexadecimal = 16)
oct (Octal = 8)
dec (Decimal = 10)
Other than the above base converters the setbase () can modify base of a variable. The hex, oct, and dec manipulator can modify base of input or output numbers.
Example:
int number = 100;
cout << "Hex Value =" << " " << hex << number << endl;
cout << "Octal Value=" << " " << oct << number << endl;
cout << "Setbase Value=" << " " << setbase(16) << number << endl;
The output of the above code is:
Hex Value = 0064
Octal Value = 144
Setbase Value= 0064
setw() and setfill()
The setw() is an output manipulator that create whitespace character between two variables. You must specify an interger value equal to the space required.
setw(int n)
The setfill() fill the whitespaces of setw() with a different character. It is an output manipulator like setw(), but the required parameter is a single character. Note that a character is enclosed in between single quotes.
setfill(char ch)
Example:
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
cout << setfill('*') << setw(25) << "*" << endl;
cout << "First Name" << setfill(' ') << setw(15) << "Last Name" << endl;
cout << "Aaron" << setw(16) << "Johns" << endl;
cout << setfill('*') << setw(25) << "*" << endl;
}
Output:
*************************
First Name Last Name
Aaron Johns
*************************
setprecision()
The setprecision() is an output manipulator which controls the number of digits to be displayed for a floating point number after the decimal point. The function is defined in the iomanip header so make sure to include this file in your program.
Example:
float A = 1.34255;
cout << setprecision(3) << A << endl;
ws
In C++, the ws manipulator is an input stream manipulator that is used to skip whitespace characters in input streams. It is defined in the <istream> header and can be used with any input stream type. The ws manipulator ignores leading whitespace characters from the input stream, which is particularly useful when reading formatted data. If it reaches the end of the file while skipping whitespace, it sets the eofbit but not the failbit, allowing for error handling based on these flags.
Example:
#include <iostream>
#include <string>
int main() {
std::string firstName, lastName;
std::cout << "First Name: ";
std::cin >> std::ws; // Skip leading whitespace
std::getline(std::cin, firstName);
std::cout << "Last Name: ";
std::cin >> std::ws; // Skip leading whitespace again
std::getline(std::cin, lastName);
std::cout << firstName << " " << lastName << "!" << std::endl;
return 0;
}
setiosflags(), resetiosflags() and flush
The setiosflags() method of iomanip library in C++ is used to set the ios library format flags specified as the parameter to this method.
Syntax:
setiosflags (ios_base::format_flag)
The resetiosflags() method of iomanip library in C++ is used to reset the ios library format flags specified as the parameter to this method.
Syntax:
resetiosflags (ios_base::format_flag)
The flush function clears the output stream. It is an output manipulator means does not work for input stream. This manipulator may be used to produce an incomplete line of output immediately, e.g. when displaying output from a long-running process, logging activity of multiple threads or logging activity of a program that may crash unexpectedly.
When a complete line of output needs to be flushed, the std::endl manipulator may be used.When every output operation needs to be flushed, the std::unitbuf manipulator may be used.
std::flush;
Example:
#include <iostream>
#include <iomanip>
int main() {
int number = 255;
std::cout << std::resetiosflags(std::ios::dec);
std::cout << std::setiosflags(std::ios::hex|std::ios::showbase|std::ios::uppercase);
std::cout << number << std::endl; // Outputs: 0Xff
std::cout << std::resetiosflags(std::ios::hex);
std::cout << number << std::endl; // Outputs: 255
std::cout << std::flush;
return 0;
}
You need to call resetiosflags before calling setiosflags. The reason for this is that setiosflags(ios::hex | ios::showbase | ios::uppercase) appends these flags to the stream, similar to calling setf(), which can lead to conflicting flags in the stream.
The most important thing to know about reading and writing files in C++ is that it is very similar to reading from the console (using cin and cout in C++ and scanf/printf in C ). The difference is that rather than having the user sit there and type characters, the program just reads the characters from the file as though they were being typed in (or rather than printing things to the screen, it prints them to a file). If you understand this concept, the rest is just syntax.
The steps to reading or writing a file are:
1) to open the file
2) to read or write the data from/to the file
3) to close the file
Opening the file requires you to supply a filename (this is just an array of characters like any other string in C/C++) and indicate whether you want to read it or write it.
Reading or writing the file is similar to reading and writing the console
Closing the file just tells the operating system that you're done with it. (Generally, forgetting to close a file is no big deal -- when your program exits, it'll usually close the file automatically. It's good style to close the file after you use it, though).
To use file I/O in C++ you typically include the iostream and fstream header files.
Example: A program which Creates a File
#include <iostream>
#include <fstream>
#include <ctime>
using namespace std;
int main(){
string a = "This File was created on ";
time_t t = time(0);
a.append(ctime(&t));
a.append("FirstName,LastName,Age\n");
fstream file;
//This line will create a new file
//It will also overwite a file if it exists
file.open("file1.csv", ios::out);
//Write the String to the file
file << a;
file.close();
return 0;
}
Example: A program which Appends Data to a File
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream file;
//This line will append data to an existing file
file.open("file1.csv", ios::app);
string fname, lname;
int age;
char c = ',';
cout << "Enter the first name, last name and age of the person:"
<< endl;
cin >> fname >> lname >> age;
file << fname << c << lname << c << age << endl;
file.close();
return 0;
}
Example: A program which reads data From a file
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
using namespace std;
int main()
{
fstream file;
//This line will read data from an existing file
file.open("file1.csv", ios::in);
string data, val;
bool first = true;
//read data from file object and put it into string.
while (getline(file, data)){
if(first){
//skip the first line
first = false;
continue;
}
stringstream ss(data);
while (getline(ss, val, ',')){
cout << setw(15) << val;
}
cout << endl;
}
}