Input and Output Data Processing Course, I. Hrivnacova, IPN Orsay Output to the Screen Input from the Keyboard IO Headers Output to a File Input from a File Formatting I. Hrivnacova @ Data Processing Course 2018 1
C++ Program /* The first C++ program * - just outputs 'Hello, World!' */ int main() // print Hello, World! on the screen cout << Hello, World! << endl; Including the necessary declarations for input and output Writing the string Hello, World! followed by std::endl symbol, to the standard output I. Hrivnacova @ Data Processing Course 2018 2
Input and Output (IO) Chua Hock-Chuan: Programming Notes C/C++ IO are based on streams, which are sequence of bytes flowing in and out of the programs (just like water and oil flowing through a pipe). In input operations, data bytes flow from an input source (such as keyboard, file, network or another program) into the program. In output operations, data bytes flow from the program to an output sink (such as console, file, network or another program). I. Hrivnacova @ Data Processing Course 2018 3
Input and Output The input/output (I/O) functionality is provided in components of standard library: Standard channels to provide input/output: Generally assigned to the keyboard (input) and the screen (output) Can be redirected by the operating system to a file The standard library defines types and objects specially designed for IO Objects: cin, cout - input channel (the keyboard), output channel (the screen) Global stream objects, they are already declared, and so available for use Types: istream, ostream : defines the type for objects that can be used for input, output Operators: <<, >> operator: Send, receive data t/fromo the output/an input channel I. Hrivnacova @ Data Processing Course 2018 4
Output to the Screen We use the stream std::cout and the operator << #include <string> int main () int i = 1; float f = 2.3; string s = "abcd"; cout << " i=" << i << " f=" << f << " s=" << s << std::endl; The operator << is intelligent: it adapts to the type of the object to its right I. Hrivnacova @ Data Processing Course 2018 5
Output to the Screen We use the stream std::cout and the operator << #include <string> int main () int i = 1; float f = 2.3; string s = "abcd"; cout << " i=" << i << " f=" << f << " s=" << s << std::endl; Program output: >./testoutput i=1 f=2.3 s=abcd I. Hrivnacova @ Data Processing Course 2018 6
Input from the Keyboard We use the stream std::cin and the operator >> #include <string> int main () int i; float f; string s; cin >> i >> f >> s; cout << " i=" << i << " f=" << f << " s=" << s << endl; The entries in cascade can be separated by any separation character: space, TAB, CR I. Hrivnacova @ Data Processing Course 2018 7
Input from the Keyboard We use the stream std::cin and the operator >> #include <string> int main () int i; float f; string s; cin >> i >> f >> s; cout << " i=" << i << " f=" << f << " s=" << s << endl; Program output: >./testinput 1 2 Blablah i=1 f=2 s=blablah I. Hrivnacova @ Data Processing Course 2018 8
IO Header Files Header File <iostream> <fstream> <iomanip> Function and Description This file defines the cin, cout, cerr and clog objects, which correspond to the standard input stream, the standard output stream, the un-buffered and buffered standard error stream, respectively. This file declares services for user-controlled file processing. This file declares services useful for performing formatted I/O with so-called parameterized stream manipulators, such as setw and setprecision. I. Hrivnacova @ Data Processing Course 2018 9
Files The file input/output (I/O) functionality is provided in components of standard library: #include <fstream> Types: ifstream, ofstream : define the types for objects that can be used for input, output They are of the same family as istream and ostream, and so the objects of this type can be manipulated the same way Using operators: <<,>> Objects There are no global file stream objects (such as cin and cout), we have to define them ourselves before we can use them We choose the names of our stream objects ifstream infile("input.txt"); ofstream outfile("output.txt"); I. Hrivnacova @ Data Processing Course 2018 10
#include <fstream> #include <string> int main () int i = 1; float f = 2.3; string s = "abcd"; ofstream outfile("file.txt"); if (! outfile.is_open() ) cerr << "Cannot open file" << endl; return 1; outfile << i << " " << f << " " << s << std::endl; outfile.close(); Output to a File The operation to open a file may fail If the output file does not exist, if one has no right to write to the directory for the output file... We have to test the result of the open operation: The operator << is used in the same way as to output to the screen I. Hrivnacova @ Data Processing Course 2018 11
#include <fstream> #include <string> int main () int i = 1; float f = 2.3; string s = "abcd"; Output to a File ofstream outfile("file.txt"); if (! outfile.is_open() ) cerr << "Cannot open file" << endl; return 1; outfile << i << " " << f << " " << s << std::endl; outfile.close(); Program output: >./testfileoutput > more file.txt 1 2.3 abcd I. Hrivnacova @ Data Processing Course 2018 12
#include <fstream> #include <string> using namespace srd; int main () int i; float f; std::string s; ifstream infile("file.txt"); if (! infile.is_open() ) cerr << "Cannot open file" << endl; return 1; infile >> i >> f >> s; Input from a File The operation to open a file may fail The input file may not exist We have to test the result of the open operation: The operator >> is used in the same way as to input from the keyboard std::cout << " i=" << i << " f=" << f << " s=" << s << std::endl; I. Hrivnacova @ Data Processing Course 2018 13
#include <fstream> #include <string> using namespace srd; Input from a File int main () int i; float f; std::string s; ifstream infile("file.txt"); if (! infile.is_open() ) cerr << "Cannot open file" << endl; return 1; infile >> i >> f >> s; Program output: >./testfileinput std::cout << " i=" << i i=1 f=2.3 s=abcd << " f=" << f << " s=" << s << std::endl; I. Hrivnacova @ Data Processing Course 2018 14
Formatting Output The <iomanip> header provides so-called I/O manipulators for formatting output (and input): setw(int field-width) setfill(char fill-char) left right internal fixed scientific setprecision(int numdecimaldigits) boolalpha noboolalpha Set the field width for the next IO operation. setw() is non-sticky and must be issued prior to each IO operation. Set the filled character for padding to the field width. left right internal: set the alignment. Set the alignment. Use fixed-point notation (e.g, 12.34) or scientific notation (e.g., 1.23e+006). For floating-point numbers. Specify the number of digits after the decimal point. For floating-point numbers. display bool values as alphabetic string (true/false) or 1/0. For bool. I. Hrivnacova @ Data Processing Course 2018 15
#include <iomanip> // Needed to do formatted I/O Formatting Output int main() // Floating point numbers double pi = 3.14159265; cout << fixed << setprecision(4); // fixed format with 4 decimal places cout << pi << endl; cout << " " << setw(8) << pi << " " << setw(10) << pi << " " << endl; // setw() is not sticky, only apply to the next operation. cout << setfill('-'); cout << " " << setw(8) << pi << " " << setw(10) << pi << " " << endl; cout << scientific; // in scientific format with exponent cout << pi << endl; // booleans bool done = false; cout << done << endl; // print 0 (for false) or 1 (for true) cout << boolalpha; // print true or false cout << done << endl; I. Hrivnacova @ Data Processing Course 2018 16
#include <iomanip> // Needed to do formatted I/O Formatting Output int main() // Floating point numbers double pi = 3.14159265; cout << fixed << setprecision(4); // fixed format with 4 decimal places cout << pi << endl; cout << " " << setw(8) << pi << " " << setw(10) << pi << " " << endl; // setw() is not sticky, only apply to the next operation. cout << setfill('-'); cout << " " << setw(8) << pi << " " << setw(10) << pi << " " << endl; cout << scientific; // in scientific format with exponent cout << pi << endl; Program output: >./testformatting 3.1416 // booleans bool done = false; 3.1416 3.1416 cout << done << endl; // print 0 (for false) or 1 (for --3.1416 ----3.1416 true) cout << boolalpha; // print true or false 3.1416e+00 cout << done << endl; 0 false I. Hrivnacova @ Data Processing Course 2018 17