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

More Related Content

What's hot

C formatted and unformatted input and output constructs
C  formatted and unformatted input and output constructsC  formatted and unformatted input and output constructs
C formatted and unformatted input and output constructsGopikaS12
 
Presentation on c structures
Presentation on c   structures Presentation on c   structures
Presentation on c structures topu93
 
Serialization/deserialization
Serialization/deserializationSerialization/deserialization
Serialization/deserializationYoung Alista
 
Friend function
Friend functionFriend function
Friend functionzindadili
 
Applet life cycle
Applet life cycleApplet life cycle
Applet life cyclemyrajendra
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).pptAlok Kumar
 
file handling c++
file handling c++file handling c++
file handling c++Guddu Spy
 
cpp input & output system basics
cpp input & output system basicscpp input & output system basics
cpp input & output system basicsgourav kottawar
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++Shyam Gupta
 
class and objects
class and objectsclass and objects
class and objectsPayel Guria
 
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Jayanshu Gundaniya
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++HalaiHansaika
 

What's hot (20)

C formatted and unformatted input and output constructs
C  formatted and unformatted input and output constructsC  formatted and unformatted input and output constructs
C formatted and unformatted input and output constructs
 
Input output streams
Input output streamsInput output streams
Input output streams
 
Oops ppt
Oops pptOops ppt
Oops ppt
 
Presentation on c structures
Presentation on c   structures Presentation on c   structures
Presentation on c structures
 
Serialization/deserialization
Serialization/deserializationSerialization/deserialization
Serialization/deserialization
 
Managing I/O in c++
Managing I/O in c++Managing I/O in c++
Managing I/O in c++
 
C++ string
C++ stringC++ string
C++ string
 
Friend function
Friend functionFriend function
Friend function
 
Applet life cycle
Applet life cycleApplet life cycle
Applet life cycle
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
 
file handling c++
file handling c++file handling c++
file handling c++
 
cpp input & output system basics
cpp input & output system basicscpp input & output system basics
cpp input & output system basics
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
 
The string class
The string classThe string class
The string class
 
Pointers
PointersPointers
Pointers
 
class and objects
class and objectsclass and objects
class and objects
 
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
 
C string
C stringC string
C string
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 

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
 
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)
 
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
 
C++ programming
C++ programmingC++ programming
C++ programming
 
Arrays In C++
Arrays In C++Arrays In C++
Arrays In C++
 
C++ Programming Language
C++ Programming Language C++ Programming Language
C++ Programming Language
 
01 c++ Intro.ppt
01 c++ Intro.ppt01 c++ Intro.ppt
01 c++ Intro.ppt
 

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
 
Write a program that converts an infix expression into an equivalent.pdf
Write a program that converts an infix expression into an equivalent.pdfWrite a program that converts an infix expression into an equivalent.pdf
Write a program that converts an infix expression into an equivalent.pdfmohdjakirfb
 
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
 
Input and output basic of c++ programming and escape sequences
Input and output basic of c++ programming and escape sequencesInput and output basic of c++ programming and escape sequences
Input and output basic of c++ programming and escape sequencesssuserf86fba
 
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
 

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++
 
Console i/o for c++
Console i/o for c++Console i/o for c++
Console i/o for 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
 
Write a program that converts an infix expression into an equivalent.pdf
Write a program that converts an infix expression into an equivalent.pdfWrite a program that converts an infix expression into an equivalent.pdf
Write a program that converts an infix expression into an equivalent.pdf
 
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
 
Input and output basic of c++ programming and escape sequences
Input and output basic of c++ programming and escape sequencesInput and output basic of c++ programming and escape sequences
Input and output basic of c++ programming and escape sequences
 
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
 

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