Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unformatted and Formatted I/O Operations. 2 Unformatted Input/output is the most basic form of input/output. Unformatted I/O transfers the internal binary.

Similar presentations


Presentation on theme: "Unformatted and Formatted I/O Operations. 2 Unformatted Input/output is the most basic form of input/output. Unformatted I/O transfers the internal binary."— Presentation transcript:

1 Unformatted and Formatted I/O Operations

2 2 Unformatted 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 3 Unformatted I/O Operations cout, cin, put(), get(), getline() write() The classes istream and ostream defines 2 member functions get() and put() To handle single character I/O operations It read a character including blank space, tab and new line. get() are 2 types get(void ) and get(char) get(char) assigns the input character to its argument get(void) returns the input character

4 4 Unformatted I/O Operations Example 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(); } Or C=cin.get(); cout.put(c);

5 5 Unformatted I/O Operations getline() Reads a whole line of text that ends with a new line character Can invoke using the object cin Syn cin.getline(character array,size); Reading is terminated as soon as either the new line or at size-1 char s[20]; cin.getlinw(s,4); Let the data inputted is popop cout<<s; will print popo

6 6 Unformatted I/O Operations write() Display a string Invoke using cout Syntax cout.write(array,size); First argument is the string to display Second argument size indicate the number of character to display It does not stop displaying the character automatically at null character If the size greater than the length of the string, then it displays beyond the bound char s[5]=“Popo”; for(int i=1;i<=8;i++) { cout.write(s,i); cout <<“\n”; } P Po Pop Popo Popo @# popo@#1 popo@#19 popo@#198

7 7 Formatted I/O Operations ios class functions and flags Manipulators User-defined output functions

8 8 Formatted I/O Operations ios class functions and flags The ios class contains a large number member functions, for format the output Some of the functions are width() Used to define the width of a field for the output of an item Syn cout.width(w); w- the field width The output will be printed in a fiels of w characters wide int a=10; cout.width(5); cout<<a; 10

9 9 Formatted I/O Operations ios class functions and flags width() int a=10,b=50; cout.width(5); cout<<a; cout.width(5); cout<<b; width(-w) -w align to left cout.width(5); cout<<a; cout.width(-5); cout<<b; 10 50 10 50

10 10 Formatted I/O Operations ios class functions and flags Precision() Used to set no of digits after decimal point cout.precision(d); d- no of digits Eg float f=1.12678; cout.precision(2); cout.width(5); cout<<f; It will be rounded 1.13

11 11 Formatted I/O Operations ios class functions and flags fill() Used to fill a character in the unused position of the field By default white spaces cout.fill(‘sign’); sign- character to fill Eg int a=10; cout.fill(‘*’); cout.width(5); cout<<a; *** 10

12 12 Formatted I/O Operations ios class functions and flags Formatting Flags, Bit-fields setf() Member function of ios class Setf stands for set flags cout.setf(arg); arg2-called bit field, specifies the group to which the formatting flags belongs Eg int a=10; cout.fill(‘*’); cout.width(5); Cout.setf(ios::left); cout<<a; MeaningFlag left ios::left right ios::right showpos ios::showpos noshowpos uppercase ios::uppercase nouppercase 10* **

13 13 Formatted I/O Operations Manipulators Include the header file iomanip.h Manipulators are used to manipulate the output formats Manipulators are functions specifically designed to be used in conjunction with the insertion ( >) operators ManipulatorsMeaning setw()set the field width to w setprecision(d)set the floating point precision to d setfill((c)set fill character to c endlnew line

14 14 Formatted I/O Operations Manipulators Eg cout<<setw(5)<<setprecision(2)<<1.2456; cout<<endl cout<<setw(10)<<setprecision(3)<<1.34567; cout << setw(10) << setfill('$') << 50 ;

15 15 Formatted I/O Operations User 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. 35 My Style


Download ppt "Unformatted and Formatted I/O Operations. 2 Unformatted Input/output is the most basic form of input/output. Unformatted I/O transfers the internal binary."

Similar presentations


Ads by Google