C plus plus Stream Input Output 1-Advanced Programming for Engineers-Lecture Slides, Slides of Programming for Engineers

This lecture was delivered by Prof. Prabhat Patel at Ankit Institute of Technology and Science for Advanced Programming for Engineers. It includes: C , Stream, Input, Output, Class, Hierarchy, File, Handling, Format, Flags, Control, Settings, Justification

Typology: Slides

2011/2012

Uploaded on 07/25/2012

hun_i
hun_i 🇮🇳

3.7

(3)

54 documents

1 / 19

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
C++ Stream Input/Output-I
Chapter-15
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13

Partial preview of the text

Download C plus plus Stream Input Output 1-Advanced Programming for Engineers-Lecture Slides and more Slides Programming for Engineers in PDF only on Docsity!

C++ Stream Input/Output-I

Chapter-

C++ Streams Class Hierarchy

ios (base class)

ostream istream

ofstream iostream ifstream

fstream

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.

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::dec use base ten

ios::oct use base eight

ios::hex use base sixteen

I/O Stream Member Functions

.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::showpos) ;

I/O Stream Member Functions

.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 docsity.com

I/O Stream Member Functions

.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

Using Manipulators & Member Functions

#include // No “.h” (standard header) #include // 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 ;

Example Program Output

Enter your name

R. J. Freuler

Enter two integers and a float

Thank you, R. J. Freuler, you entered

12, 24, and •• 68

Thank you, R. J. Freuler, you entered

12, 24, and •••67.

More Input Stream Member Functions

.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"

More Input Stream Member Functions

.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.

More Input Stream Member Functions

.ignore ( ) ;

Ex:

cin.ignore ( ) ; // gets and discards 1 character

cin.ignore ( 2 ) ; // gets and discards 2 characters

File I/O with C++

#include 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 }