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< { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/20/5940942/slides/slide_4.jpg", "name": "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<

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< { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/20/5940942/slides/slide_5.jpg", "name": "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<

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< { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/20/5940942/slides/slide_8.jpg", "name": "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<

9 9 Formatted I/O Operations ios class functions and flags width() int a=10,b=50; cout.width(5); cout< { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/20/5940942/slides/slide_9.jpg", "name": "9 Formatted I/O Operations ios class functions and flags width() int a=10,b=50; cout.width(5); cout<

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< { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/20/5940942/slides/slide_10.jpg", "name": "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<

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< { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/20/5940942/slides/slide_11.jpg", "name": "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<

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< { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/20/5940942/slides/slide_12.jpg", "name": "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<

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< { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/20/5940942/slides/slide_14.jpg", "name": "14 Formatted I/O Operations Manipulators Eg cout<

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< { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/20/5940942/slides/slide_15.jpg", "name": "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<


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