C++

Topic: I/o operations

How to Formatted Input/Output ?

Generating cleanly formatted output is a common programming requirement. Formatting improves your user interface and makes it easier to read any debugging messages that you might output to the screen. In C++, the I/O formatting can be performed by either manipulators or using the methods of I/O classes and format-state flags (a detailed overview can be found here). We will use manipulators in these notes.A manipulator is a function that can alter the characteristics of the output (and input) stream. For cout, we have seen the endl manipulator which ends a line (and calls the flush function of the ostream).Using cout to Format OutputThe following manipulators, defined in the header file <iomanip>, are the most commonly used for formatting output.    setw(n) : sets the minimum width of the next output. The length of the width is n.    setfill(c) : fills leading spaces in a number with a given character c.    setprecision(n) : sets the maximum number of digits (given by n) that are displayed for a number.    fixed : allows inserting floating-point values in fixed format    scientific : allows inserting floating-point values in scientific format    left : left-justify    right : right-justify    dec : insert or extract integer values in decimal format    oct : insert or extract values in octal (base 8) format    hex : insert or extract integer values in hexadecimal (base 16) format For example, the following program section  int   i = 1299;  float f = 314.15926;  cout << "numbers:" << setw(10) << i << f << "four" << endl;will output         1         2         312345678901234567890123456789012345numbers:      1299314.159fourThe first two lines are not part of the output, they are just for gauging the output. After outputting "numbers:", setw(10) creates a 10-column empty field and then the number 1299 is placed in this field right-justified. This is followed by "314.159four". Note that 314.15926 is output as "314.159" because of the default format of the compiler.For the following assignment of variables  int    i = 1453;  float  f = 314.15926;  string s = "University of Gaziantep";Using cin to Format Inputcin, being responsible for input, is a class defined in <iostream>. The extraction operator (>>) is used to put data into your program's variables. For example, the following operation  int var;  cin >> var;writes data from the buffer of cin to your local variable, var.cin has some member functions to manipulate input data. The famous one is get() that is used to get a single character, including a white space, or a string for input. For example, the following program section  char a, b, c;  cout << "Enter three letters: ";  cin.get(a).get(b).get(c);  cout << a << " " <<  b << " " << c;will output  Enter three letters: one  o n eHere is the another usage  char str1[256], str2[256];  cout << "Enter string one: ";  cin.get(str1, 256);  cout << "string one: " << str1 << endl;  cout << "Enter string two: ";  cin >> str2;  cout << "string two: " << str2 << endl;an example I/O is  Enter string one: Now is the time  string one: Now is the time  Enter string two: Now is the time  string two: NowIn the second input str2 is assigned a string from a space-delimited input so only the first word is stored.Note that the getline() function can be used to input a whole line into a string:  string s;  cout << "Input your message: ";  getline(cin, s);

Browse random answers:

What are Basic Input/Output ?
How to write c++ program to write a message to the screen?
Why do we need to close a file.Eg.ifstream ifile;ifile.open("temp.dat").....ifile.close();What happens if we do not close() a file?
Can we make any program in c++ without using any header file and what is the shortest program in c++.?
Bitwise Operations - Given inputs X, Y, Z and operations | and & (meaning bitwise OR and AND, respectively), what is output equal to in?
What is the output of printf ("%d")
What will be output of the following code ?#includeusing namespace std;class abc{public :void main(){cout<<" Its main function "< }};int main(int c, char **v){abc a;if(c<1){cout<<" Error can not accept it "< exit(1);}cout<<" its in main program "< a.main();return 0;}
Write a C++ program that uses a loop to sum ten numbers read from the standard input, and then writes the sum to the standard output ?
How to write a code for replacing a character with astrick sign?
Explain about File I/O ?
Input output fuction in c plus plus?
How do you write a program which produces its own source code as its output?
Is it possible to have different access modifiers on the get/set methods of a property?
Explainabout Standard input and output ?
I was creating a program that has a 2 dimensional array with 25 elements.How can I display the highest row sum?lowest row sum?highest col sum?lowest row sum?
HOW TO READ TEXT FILE AND READ IT INTO ANOTHER TEXT FILE?
Write a C++ program that includes two functions ?
What are the different formats specifiers available for input and output statements ?
Explain about Standard Output (cout) ?
Explain about Input/Output with files ?
How to Open a file in input and output ?
How to Closing a file in i/o ?
How to Text files in i/o ?
How to Checking state flags in i/o ?
How to get and put stream pointers in i/o ?
Explain about Binary files in i/o ?
What are Header files i/o ?
How to getting a stream in i/o ?
How to Passing streams to functions in i/o ?
What are Item by item input and output ?
What are Other input operations ?
What are Repositioning and error states in i/o ?
What are standard error stream in i/o ?
standard log stream (clog) in i/o ?
How to Formatted Input/Output ?
Explain about cstdio (stdio.h) ?
Write a Text File  in i/o ?
How to Monitor File System Changes in i/o ?
Explain about extending the I/O System
How to  Enumerate Files in a Directory  in i/o ?
What are I/O Modes ?