Top Banner
C++ uses the concept of stream and stream classes to implement its I/O operations with the console oriented input-output operations.
27

object oriented programming c++ input/ output streams slides

Nov 24, 2015

Download

Documents

ricketbus

how to work on input/output streams in c++, file handling
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • C++ uses the concept of stream and stream classes to implement its I/O operations with the console oriented input-output operations.

  • The I/O system in c++ is designed to work with a wide variety of devices including terminals, disks. Although each device is very different, the I/O system supplies an interface to the programmer that is independent of the actual device being accessed. This interface is known as STREAM.

  • A stream is a sequence of bytes. It acts either as a source from which the input data can be obtained or as a destination to which the output data can be sent.The source stream that provides data to the program is called the input stream and the destination stream that receives output from the program is called the output stream. A program extracts the bytes from an input stream and inserts bytes into an output stream.

  • Input deviceOutput deviceprogramOutput streamInput streamExtraction from input streamInsertion into output stream

  • The data in the input stream can come from the keyboard or any other storage device. Similarly the data in the output stream can go to the screen or any other storage device. So a stream acts as an interface between the program and the input/output device.

  • C++ contains several pre-defined streams that are automatically opened when a program begins its execution. Ex:cincoutcin represents the input stream connected to the standard input device (usually the keyboard) cout represents the output stream connected to the standard output device (usually screen).

  • Class nameContentsios(General input/output stream class)Contains basic facilities that are used by all other input and output classes.istream(input stream)Inherits properties of iosDeclares input functions such as get(), getline()Contains overloaded extraction operator >>Ostream (output stream)Inherits properties of ios.Declares output functions put() and write()Contains overloaded insertion operator

  • The >> operator is overloaded in the istream class and >variable1>>variable2>>>>variableNVariable1,variable2 and .. Are valid variable names that have been declared already.The operator >> reads the data character by character and assigns it to the indicated location. The reading will be terminated at the encounter of a white space or a character that does not match the destination type.

  • int code;cin>>code;

    4258D

  • get(void)Version returns the input character.get(char *)Example:char c;cin.get(c);Or c=cin.get();

  • The function put(), a member of ostream class, can be used to output a line of text, character by character.cout.put(x);

  • The getline() function reads a whole line of text that ends with a newline character. cin.getline(line,size);Example:char name[20];cin.getline(name,20);I/P:Object oriented ProgrammingO/P: Object Oriented Pro

  • The write() funciton displays an entire line and has the following form:cout.write(line,size);

  • FunctionsTaskWidth()To specify the required field size for displaying an output valueprecision()To specify the number of digits to be displayed after the decimal point of a float value.fill()To specify a character that is used to fill the unused portion of a field.setf()To specify format flags that can control the form of output display.unsetf()To clear the flags specified.

  • The width() function to define the width of a field necessary for the output of a an item.cout.width(w);Where w is the field width.Example:cout.width(5);cout
  • The width() function to define the width of a field necessary for the output of a an item.cout.width(w);Where w is the field width.Example:cout.width(5);cout
  • cout.width(5);cout
  • By default, the floating numbers are printed with six digits after the decimal point. cout.precision(d);cout.precision(3);cout
  • The output is rounded to the nearest cent.Trailing zeros are truncated.Precision setting stays in effect until it is reset.Default precision is 6 digits.Set to default.Cout.precision(0);

  • By default, the unused positions of the field are filled with white spaces.Use the fill() to fill the unused positions by any desired character. cout.fill(ch);

  • cout.fill('*');cout.width(10);cout
  • The setf()(set flags)cout.setf(arg1,arg2)

  • Format requiredFlag(arg1)Bit-field(arg2)Left-justified output ios::leftios::adjustfieldRight-justified outputios::rightios::adjustfieldPadding after signios::internalios::adjustfieldScientifc notationios::scientificios::floatfieldFixed point notations ios::fixedios::floatfield

  • cout.fill(*);cout.setf(ios::left,ios::adjustfield);cout.width(8);cout
  • cout.width(10);cout.setf(ios::internal);cout.fill('*');cout
  • ManipulatorEquivalent ios functionsetw()width()setprecision()precision()setfill()fill()setiosflags()setf()resetiosflags()unsetf()