SlideShare a Scribd company logo
1 of 23
Download to read offline
Lect 27	P. 1,[object Object],Winter Quarter,[object Object],C++ I/O Manipulation,[object Object]
Lect 27	P. 2,[object Object],Note:	There is no “.h” on standard header files.,[object Object],			Be careful about “using namespace std”,[object Object],iostream -- contains basic information required for all stream I/O operations,[object Object],iomanip -- contains information useful for performing formatted I/O with parameterized stream manipulators,[object Object],fstream -- contains information for performing file I/O operations,[object Object],strstream -- contains information for performing in-memory I/O operations (i.e., into or from strings in memory),[object Object],Winter Quarter,[object Object],Stream I/O Library Header Files,[object Object]
Lect 27	P. 3,[object Object],ios is the base class.,[object Object],istream and ostream inherit from ios,[object Object],ifstream inherits from istream (and ios),[object Object],ofstream inherits from ostream (and ios),[object Object],iostream inherits from istream and ostream (& ios),[object Object],fstream inherits from ifstream, iostream, and ofstream,[object Object],Winter Quarter,[object Object],Classes for Stream I/O in C++,[object Object]
Lect 27	P. 4,[object Object],C++ provides various stream manipulators that perform formatting tasks.,[object Object],Stream manipulators are defined in <iomanip>,[object Object],These manipulators provide capabilities for,[object Object],setting field widths,,[object Object],setting precision,,[object Object],setting and unsetting format flags,,[object Object],flushing streams,,[object Object],inserting a "newline" and flushing output stream,,[object Object],skipping whitespace in input stream,[object Object],Winter Quarter,[object Object],C++ Stream I/O -- Stream Manipulators,[object Object]
Lect 27	P. 5,[object Object],setprecision ( ),[object Object],Select output precision, i.e., number of significant digits to be printed.,[object Object],Example:,[object Object],	cout << setprecision (2) ;   // two significant digits,[object Object],setw ( ) ,[object Object],Specify the field width (Can be used on input or output, but only applies to next insertion or extraction).,[object Object],Example:,[object Object],	cout << setw (4) ;	// field is four positions wide,[object Object],Winter Quarter,[object Object],C++ Stream I/O -- Stream Manipulators,[object Object]
Lect 27	P. 6,[object Object],Winter Quarter,[object Object],C++ Stream I/O -- Stream Format States,[object Object],Various ios format flags specify the kinds of formatting to be performed during stream I/O.  ,[object Object],There are member functions (and/or stream manipulators) which control flag settings.,[object Object],There are various flags for trailing zeros and decimal points, justification, number base, floating point representation, and so on.,[object Object]
Lect 27	P. 7,[object Object],Stream I/O Format State Flags,[object Object],ios::showpoint	when set, show trailing decimal,[object Object],				 point and zeros,[object Object],ios::showpos	when set, show the + sign before,[object Object],				positive numbers,[object Object],ios::basefield		,[object Object],	ios::dec		use base ten,[object Object],    	ios::oct		use base eight,[object Object],	ios::hex		use base sixteen,[object Object],Winter Quarter,[object Object]
Lect 27	P. 8,[object Object],Winter Quarter,[object Object],Stream I/O Format State Flags,[object Object],ios::floatfield,[object Object],	ios::fixed		use fixed number of digits,[object Object],	ios::scientific	use "scientific" notation,[object Object],ios::adjustfield,[object Object],	ios::left		use left justification,[object Object],	ios::right		use right justification,[object Object],	ios::internal	left justify the sign, but right,[object Object],				justify the value ,[object Object], ,[object Object]
Lect 27	P. 9,[object Object],ios::eofbit	set when eof encountered [ stream.eof() ],[object Object],ios::failbit	set when format error occurred	on the,[object Object],			stream, but no characters were lost,[object Object],			[ stream.fail() or simply ! stream ],[object Object],ios::badbit	set when stream error occurs that,[object Object],			results in a loss of data [ stream.bad() ],[object Object],ios::goodbit	set when none of the bits eofbit,,[object Object],failbit, or badbit are set [ stream.good() ],[object Object],Winter Quarter,[object Object],Stream I/O Format State Flags,[object Object]
Lect 27	P. 10,[object Object],.setf ( ),[object Object],Allows the setting of an I/O stream format flag.,[object Object],Examples:,[object Object],	// To show the + sign in front of positive numbers,[object Object],cout.setf (ios::showpos) ; ,[object Object],	// To output the number in hexadecimal,[object Object],	cout.setf (ios::hex, ios::basefield) ;  ,[object Object],Winter Quarter,[object Object],I/O Stream Member Functions,[object Object]
Lect 27	P. 11,[object Object],.precision ( ) ;,[object Object],Select output precision, i.e., number of significant digits to be printed.,[object Object],Example:,[object Object],		cout.precision (2) ;    // two significant digits,[object Object],.width ( ) ;,[object Object],Specify field width. (Can be used on input or output, but only applies to next insertion or extraction).,[object Object],Example:,[object Object],		cout.width (4) ;	// field is four positions wide,[object Object],Winter Quarter,[object Object],I/O Stream Member Functions,[object Object]
Lect 27	P. 12,[object Object],.eof ( ) ;,[object Object],Tests for end-of-file condition.,[object Object],Example:,[object Object],		cin.eof ( ) ;    // true if eof encountered,[object Object],.fail ( ) ;,[object Object],Tests if a stream operation has failed.,[object Object],Example:,[object Object],		cin.fail ( ) ;	// true if a format error occurred,[object Object],		! cin;		// same as above; true if format error,[object Object],Winter Quarter,[object Object],I/O Stream Member Functions,[object Object]
Lect 27	P. 13,[object Object],.clear ( ) ;,[object Object],Normally used to restore a stream's state to "good" so that I/O may proceed or resume on that stream. ,[object Object],Example:,[object Object],		cin.clear ( ) ;    // allow I/O to resume on a "bad" ,[object Object],                                    // stream, in this case "cin", ,[object Object],                                    // on which error had previously,[object Object],				   // occurred,[object Object],Winter Quarter,[object Object],I/O Stream Member Functions,[object Object]
Lect 27	P. 14,[object Object],#include <iostream>	// No “.h” (standard header),[object Object],#include <iomanip>	// No “.h” (standard header),[object Object],using namespace std;	// To avoid “std::”,[object Object],int main ( ),[object Object],{,[object Object],int a, b, c = 8, d = 4 ;,[object Object],float k ;,[object Object],char name[30] ;,[object Object],cout<<  "Enter your name"  <<endl;,[object Object],   cin.getline (name, 30);,[object Object],cout<< "Enter two integers and a float " <<endl;,[object Object],   cin>>  a  >>  b  >>  k ;,[object Object],Winter Quarter,[object Object],Using Manipulators & Member Functions,[object Object]
Lect 27	P. 15,[object Object],// Now, let's output the values that were read in,[object Object],cout<< "Thank you, " << name <<  ", you entered",[object Object],<<endl<<  a  <<   ", "  <<  b  <<  ", and " ;,[object Object],cout.width (4) ;,[object Object],cout.precision (2) ;,[object Object],cout<< k <<endl;,[object Object],//  Control the field and precision another way,[object Object],cout<<"Thank you, " << name << ", you entered",[object Object],<<endl<<  a << ", " <<  b << ", and " <<setw(c),[object Object],<<setprecision(d)<< k <<endl; ,[object Object],},[object Object],Winter Quarter,[object Object],Using Manipulators & Member Functions,[object Object]
Lect 27	P. 16,[object Object],Winter Quarter,[object Object],Example Program Output,[object Object],Enter your name ,[object Object],R. J. Freuler                          ,[object Object],Enter two integers and a float       ,[object Object],12  24  67.85   ,[object Object],Thank you, R. J. Freuler, you entered  ,[object Object],12, 24, and 68                       ,[object Object],Thank you, R. J. Freuler, you entered  ,[object Object],12, 24, and 67.85,[object Object]
Lect 27	P. 17,[object Object],.get ( ) ;,[object Object],Example:,[object Object],	char ch ;,[object Object],ch= cin.get ( ) ;	// gets one character from keyboard,[object Object],				// & assigns it to the variable "ch",[object Object],.get (character) ;,[object Object],Example:,[object Object],	char ch ;,[object Object],	cin.get (ch) ;	// gets one character from,[object Object],				// keyboard & assigns to "ch",[object Object],Winter Quarter,[object Object],More Input Stream Member Functions,[object Object]
Lect 27	P. 18,[object Object],Winter Quarter,[object Object],More Input Stream Member Functions,[object Object],.get (array_name, max_size) ;,[object Object],Example:,[object Object],char name[40] ;,[object Object],cin.get(name, 40) ;	// Gets up to 39 characters,[object Object],			// and inserts a null at the end of the,[object Object],			// string "name".   If a delimiter is ,[object Object],			// found, the read terminates.  The,[object Object],			// delimiter is not stored in the array, 			// but it is left in the stream. ,[object Object]
Lect 27	P. 19,[object Object],.getline (array_name, max_size) ;,[object Object],Example:,[object Object],char name[40] ;,[object Object],cin.getline(name, 40) ;  // Gets up to 39 characters,[object Object],			// and assigns the string to "name".  A,[object Object],			// null is inserted at the end of the string.,[object Object],			// Note that if a delimiter is found,,[object Object],			// it is removed from the stream, but it is,[object Object],			// not stored in the character array. ,[object Object],Winter Quarter,[object Object],More Input Stream Member Functions,[object Object]
Lect 27	P. 20,[object Object],More Input Stream Member Functions,[object Object],.ignore ( ) ;,[object Object],Ex:,[object Object],cin.ignore ( ) ;	// gets and discards 1 character,[object Object],cin.ignore(2) ;	// gets and discards 2 characters,[object Object],cin.ignore (80, '');	// gets and discards up to 80,[object Object],			              	// characters or until "newline",[object Object],					// character, whichever comes,[object Object],					// first,[object Object],Winter Quarter,[object Object]
Lect 27	P. 21,[object Object],More Input Stream Member Functions,[object Object],.peek( ) ;,[object Object],Ex:,[object Object],char ch ;,[object Object],	ch = cin.peek ( ) ;   // peek at (don't take) character,[object Object],.putback( ) ;,[object Object],Ex:,[object Object],char ch;,[object Object],cin.putback(ch);  // put character back in stream,[object Object],Winter Quarter,[object Object]
Lect 27	P. 22,[object Object],More I/O Stream Member Functions,[object Object],Winter Quarter,[object Object],.read(  ) ;  .write( ) ;,[object Object],Ex:,[object Object],char gross[144] ;,[object Object],cin.read(gross,144); // reads 144 characters from,[object Object],			                   // input stream.  Does NOT,[object Object],                                         // append '',[object Object]
Lect 27	P. 23,[object Object],File I/O with C++,[object Object],#include <fstream>,[object Object],using namespace std;,[object Object],int main ( )  {,[object Object],int a, b, c ;,[object Object],ifstream fin ;	//Create file input stream object,[object Object], 	fin.open( "my_input.dat");	//Open input file,[object Object],	fin >> a >> b ;	//Read two values from input file,[object Object],   	c = a + b ;,[object Object],ofstream fout ;	//Create file output stream object,[object Object], 	fout.open( "my_output.dat"); 	//Open output file,[object Object],	fout << c <<endl;	//Write result to output file,[object Object],	fin.close ( ) ;	//Close input file,[object Object],  	fout.close( ) ;	//Close output file,[object Object],},[object Object],Winter Quarter,[object Object]

More Related Content

What's hot (20)

Java swing
Java swingJava swing
Java swing
 
Threads in python
Threads in pythonThreads in python
Threads in python
 
Managing input and output operation in c
Managing input and output operation in cManaging input and output operation in c
Managing input and output operation in c
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
 
Console i/o for c++
Console i/o for c++Console i/o for c++
Console i/o for c++
 
Singly & Circular Linked list
Singly & Circular Linked listSingly & Circular Linked list
Singly & Circular Linked list
 
3.9 external sorting
3.9 external sorting3.9 external sorting
3.9 external sorting
 
Dynamic Memory Allocation(DMA)
Dynamic Memory Allocation(DMA)Dynamic Memory Allocation(DMA)
Dynamic Memory Allocation(DMA)
 
SQLITE Android
SQLITE AndroidSQLITE Android
SQLITE Android
 
Inheritance and polymorphism
Inheritance and polymorphism   Inheritance and polymorphism
Inheritance and polymorphism
 
Circular link list.ppt
Circular link list.pptCircular link list.ppt
Circular link list.ppt
 
Logical micro-operations
Logical micro-operationsLogical micro-operations
Logical micro-operations
 
Control flow statements in java
Control flow statements in javaControl flow statements in java
Control flow statements in java
 
Python: Modules and Packages
Python: Modules and PackagesPython: Modules and Packages
Python: Modules and Packages
 
File Input & Output
File Input & OutputFile Input & Output
File Input & Output
 
File handling in C
File handling in CFile handling in C
File handling in C
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
Dynamic memory allocation in c++
Dynamic memory allocation in c++Dynamic memory allocation in c++
Dynamic memory allocation in c++
 
Array in c++
Array in c++Array in c++
Array in c++
 
file system in operating system
file system in operating systemfile system in operating system
file system in operating system
 

Viewers also liked (20)

Manipulators
ManipulatorsManipulators
Manipulators
 
Formatted input and output
Formatted input and outputFormatted input and output
Formatted input and output
 
Managing console
Managing consoleManaging console
Managing console
 
file handling c++
file handling c++file handling c++
file handling c++
 
Input and output in C++
Input and output in C++Input and output in C++
Input and output in C++
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
 
Files in c++ ppt
Files in c++ pptFiles in c++ ppt
Files in c++ ppt
 
Chap 12(files)
Chap 12(files)Chap 12(files)
Chap 12(files)
 
cpp input & output system basics
cpp input & output system basicscpp input & output system basics
cpp input & output system basics
 
Managing console input and output
Managing console input and outputManaging console input and output
Managing console input and output
 
18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
File operations in c
File operations in cFile operations in c
File operations in c
 
Dynamic memory allocation(memory,allocation,memory allocatin,calloc,malloc,re...
Dynamic memory allocation(memory,allocation,memory allocatin,calloc,malloc,re...Dynamic memory allocation(memory,allocation,memory allocatin,calloc,malloc,re...
Dynamic memory allocation(memory,allocation,memory allocatin,calloc,malloc,re...
 
C++ Files and Streams
C++ Files and Streams C++ Files and Streams
C++ Files and Streams
 
Structures in c++
Structures in c++Structures in c++
Structures in c++
 
Virtual function
Virtual functionVirtual function
Virtual function
 
Presentation on nesting of loops
Presentation on nesting of loopsPresentation on nesting of loops
Presentation on nesting of loops
 
Basic c++ programs
Basic c++ programsBasic c++ programs
Basic c++ programs
 
Constructors & destructors
Constructors & destructorsConstructors & destructors
Constructors & destructors
 

Similar to C++ io manipulation

streams and files
 streams and files streams and files
streams and filesMariam Butt
 
programming language in c&c++
programming language in c&c++programming language in c&c++
programming language in c&c++Haripritha
 
Lec 47.48 - stream-files
Lec 47.48 - stream-filesLec 47.48 - stream-files
Lec 47.48 - stream-filesPrincess Sam
 
C programming session 01
C programming session 01C programming session 01
C programming session 01Dushmanta Nath
 
C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)Saifur Rahman
 
Managing console i/o operation,working with files
Managing console i/o operation,working with filesManaging console i/o operation,working with files
Managing console i/o operation,working with filesramya marichamy
 
Managing,working with files
Managing,working with filesManaging,working with files
Managing,working with fileskirupasuchi1996
 
C++ Advanced
C++ AdvancedC++ Advanced
C++ AdvancedVivek Das
 
THE IO LIBRARY in C++
THE IO LIBRARY in C++THE IO LIBRARY in C++
THE IO LIBRARY in C++Prof Ansari
 
C Programming ppt for beginners . Introduction
C Programming ppt for beginners . IntroductionC Programming ppt for beginners . Introduction
C Programming ppt for beginners . Introductionraghukatagall2
 
Rust LDN 24 7 19 Oxidising the Command Line
Rust LDN 24 7 19 Oxidising the Command LineRust LDN 24 7 19 Oxidising the Command Line
Rust LDN 24 7 19 Oxidising the Command LineMatt Provost
 
Data Input and Output
Data Input and OutputData Input and Output
Data Input and OutputSabik T S
 
2. Data, Operators, IO (5).ppt
2. Data, Operators, IO (5).ppt2. Data, Operators, IO (5).ppt
2. Data, Operators, IO (5).pptElisée Ndjabu
 
Os Vanrossum
Os VanrossumOs Vanrossum
Os Vanrossumoscon2007
 
2. Data, Operators, IO.ppt
2. Data, Operators, IO.ppt2. Data, Operators, IO.ppt
2. Data, Operators, IO.pptswateerawat06
 

Similar to C++ io manipulation (20)

streams and files
 streams and files streams and files
streams and files
 
Python 3000
Python 3000Python 3000
Python 3000
 
programming language in c&c++
programming language in c&c++programming language in c&c++
programming language in c&c++
 
Lec 47.48 - stream-files
Lec 47.48 - stream-filesLec 47.48 - stream-files
Lec 47.48 - stream-files
 
C programming session 01
C programming session 01C programming session 01
C programming session 01
 
C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)
 
Managing console i/o operation,working with files
Managing console i/o operation,working with filesManaging console i/o operation,working with files
Managing console i/o operation,working with files
 
Managing,working with files
Managing,working with filesManaging,working with files
Managing,working with files
 
C++ Advanced
C++ AdvancedC++ Advanced
C++ Advanced
 
THE IO LIBRARY in C++
THE IO LIBRARY in C++THE IO LIBRARY in C++
THE IO LIBRARY in C++
 
File handling in c++
File handling in c++File handling in c++
File handling in c++
 
C Programming ppt for beginners . Introduction
C Programming ppt for beginners . IntroductionC Programming ppt for beginners . Introduction
C Programming ppt for beginners . Introduction
 
About Go
About GoAbout Go
About Go
 
Operator overloading
Operator overloading Operator overloading
Operator overloading
 
2. data, operators, io
2. data, operators, io2. data, operators, io
2. data, operators, io
 
Rust LDN 24 7 19 Oxidising the Command Line
Rust LDN 24 7 19 Oxidising the Command LineRust LDN 24 7 19 Oxidising the Command Line
Rust LDN 24 7 19 Oxidising the Command Line
 
Data Input and Output
Data Input and OutputData Input and Output
Data Input and Output
 
2. Data, Operators, IO (5).ppt
2. Data, Operators, IO (5).ppt2. Data, Operators, IO (5).ppt
2. Data, Operators, IO (5).ppt
 
Os Vanrossum
Os VanrossumOs Vanrossum
Os Vanrossum
 
2. Data, Operators, IO.ppt
2. Data, Operators, IO.ppt2. Data, Operators, IO.ppt
2. Data, Operators, IO.ppt
 

More from Pedro Hugo Valencia Morales (10)

Árboles como Estructura de Datos
Árboles como Estructura de DatosÁrboles como Estructura de Datos
Árboles como Estructura de Datos
 
Colas de prioridad
Colas de prioridadColas de prioridad
Colas de prioridad
 
Heaps & priority queues
Heaps & priority queuesHeaps & priority queues
Heaps & priority queues
 
Tema4 programación generica
Tema4   programación genericaTema4   programación generica
Tema4 programación generica
 
Arboles03
Arboles03Arboles03
Arboles03
 
Arboles02
Arboles02Arboles02
Arboles02
 
Arboles01
Arboles01Arboles01
Arboles01
 
Arquitectura ssdd
Arquitectura ssddArquitectura ssdd
Arquitectura ssdd
 
Cap02 modelos1
Cap02 modelos1Cap02 modelos1
Cap02 modelos1
 
Chapter 1 slides
Chapter 1 slidesChapter 1 slides
Chapter 1 slides
 

C++ io manipulation

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.