Upload
laurel-thomas
View
214
Download
1
Embed Size (px)
Citation preview
Unformatted and Formatted I/O Operations
2
Unformatted and Formatted I/O OperationsUnformatted Input/output is the most basic form
of input/output. Unformatted I/O transfers the internal binary
representation of the data directly between memory and the file.
Formatted output converts the internal binary representation of the data to ASCII characters which are written to the output file.
Formatted input reads characters from the input file and converts them to internal form.
3
Unformatted I/O Operationscout, cin, put(), get(), getline() write()The classes istream and ostream defines 2 member
functions get() and put()To handle single character I/O operationsIt read a character including blank space, tab and
new line.get() are 2 typesget(void ) and get(char)get(char) assigns the input character to its argumentget(void) returns the input character
4
Unformatted I/O OperationsExample get(char) and put(char)class A{ char c,s[200],i; public: void read() { i=1; while(c!=‘*’) { cin.get(c); s[i]=c; i++; } s[i]=‘\0’; cout<<s; }};
void main(){ A obj; obj.read();}
OrC=cin.get();
cout.put(c);
5
Unformatted I/O Operationsgetline()Reads a whole line of text that ends with a new line
characterCan invoke using the object cinSyncin.getline(character array,size);Reading is terminated as soon as either the new line or at
size-1char s[20];cin.getlinw(s,4);Let the data inputted is popopcout<<s; will print popo
6
Unformatted I/O Operationswrite()Display a string Invoke using coutSyntaxcout.write(array,size);First argument is the string to displaySecond argument size indicate the number of character to displayIt does not stop displaying the character automatically at null characterIf the size greater than the length of the string, then it displays beyond the
boundchar s[5]=“Popo”;for(int i=1;i<=8;i++){ cout.write(s,i); cout <<“\n”;}
PPoPopPopoPopo @#popo@#1popo@#19popo@#198
7
Formatted I/O Operations
ios class functions and flags
Manipulators
User-defined output functions
8
Formatted I/O Operations ios class functions and flagsThe ios class contains a large number member functions ,
for format the outputSome of the functions arewidth()Used to define the width of a field for the output of an itemSyncout.width(w); w- the field widthThe output will be printed in a fiels of w characters wideint a=10;cout.width(5);cout<<a;
1 0
9
Formatted I/O Operationsios class functions and flagswidth()int a=10,b=50;cout.width(5);cout<<a;cout.width(5);cout<<b;
width(-w) -w align to leftcout.width(5);cout<<a;cout.width(-5);cout<<b;
1 0 5 0
1 0 5 0
10
Formatted I/O Operationsios class functions and flagsPrecision()Used to set no of digits after decimal pointcout.precision(d); d- no of digits Egfloat f=1.12678;cout.precision(2);cout.width(5);cout<<f;It will be rounded
1 . 1 3
11
Formatted I/O Operationsios class functions and flagsfill()Used to fill a character in the unused position of the fieldBy default white spacescout.fill(‘sign’); sign- character to fill Egint a=10;cout.fill(‘*’);cout.width(5);cout<<a;
* * * 1 0
12
Formatted I/O Operationsios class functions and flagsFormatting Flags, Bit-fieldssetf() Member function of ios classSetf stands for set flagscout.setf(arg);arg2-called bit field, specifies the group to which the formatting flags
belongsEgint a=10;cout.fill(‘*’);cout.width(5);Cout.setf(ios::left);cout<<a;
Meaning Flagleft ios::leftright ios::rightshowpos ios::showposnoshowposuppercase ios::uppercasenouppercase
1 0 * * *
13
Formatted I/O OperationsManipulatorsInclude the header file iomanip.hManipulators are used to manipulate the output formatsManipulators are functions specifically designed to be
used in conjunction with the insertion (<<) and extraction (>>) operators
Manipulators Meaningsetw() set the field width to wsetprecision(d) set the floating point precision to dsetfill((c) set fill character to cendl new line
14
Formatted I/O OperationsManipulatorsEgcout<<setw(5)<<setprecision(2)<<1.2456;cout<<endlcout<<setw(10)<<setprecision(3)<<1.34567;cout << setw(10) << setfill('$') << 50 ;
15
Formatted I/O OperationsUser defined manipulators• The user defined manipulators are defined as follows:• ostream & manipulator(ostream & ostr)
{set of statements;return ostr;}eg
• ostream & mystyle(ostream & c)• {• cout<<“My Style”;• cout<<setw(5)<<setprecision(2);• return(c);• }
• float a=1.34566;• cout<<mystyle<<a;
1 . 3 5My Style