Basic Io operations

Source: Internet
Author: User
Tags setf
Requirements:
Understanding C ++ stream operation functions
Understanding the two methods of I/O Stream format Control

Basic output stream (cout) Operations
1. Use the stream insertion operator (<)
# Include <iostream. h>
Void main ()
{
Char C [] = "good morning ";
Cout <C <Endl;
}
Running result:
Good morning
Press any key to continue
2. Use member functions
// Ostream & put (char );
// Output one character for continuous use
# Include <iostream. h>
Void main ()
{
Cout. Put ('A'). Put ('\ n ');
}
Running result:
A
Press any key to continue
// Ostream & write (const char *, INT );
// Output several characters
# Include <iostream. h>
Void main ()
{
Char C [] = "good morning ";
Cout. Write (C, 4) <Endl;
}
Running result:
Good
Press any key to continue
Input stream (CIN) basic operations
1. Use the extract operator (>)
// Separate the input values with spaces, tabs, and carriage return.
# Include <iostream. h>
Void main ()
{
Int I, j; char C;
Cin> I> C> J;
Cout <I <"[" <C <"]" <j <Endl;
}
Running result:
123 456
123 [4] 56
Press any key to continue
2. Use member functions
// Int get ();
// The EOF (-1) is returned when the read end mark is reached)
# Include <iostream. h>
Void main ()
{
While (true)
{
Cout <cin. Get () <'\\';
If (CIN. EOF () break;
}
}
Running result:
12 34 5
49 \ 50 \ 32 \ 51 \ 52 \ 9 \ 53 \ 10 \ ^ Z
-1 \ press any key to continue
// Istream & get (char &);
// The NULL pointer is returned for the read end mark.
# Include <iostream. h>
Void main ()
{
Char C;
While (true)
{
Cout <cin. Get (c) <Endl;
If (CIN. EOF () break;
}
}
Running result:
12
0x004300ec
0x004300ec
0x004300ec
^ Z
Zero X 00000000
Press any key to continue
// Istream & get (char *, int N, char delimit = '\ n ');
// Get does not extract the terminator from the stream. The default Terminator is carriage return.
# Include <iostream. h>
Void main ()
{
Char S1 [10], S2 [10], S3 [10];
Cin. Get (S1, 10 );
Cin. Get (S2, 10 );
Cin. Get (S3, 10 );
Cout <"------------------- \ n ";
Cout <S1 <Endl;
Cout <S2 <Endl;
Cout <S3 <Endl;
}
Running result:
1234567890 abcdef
---------------------
123456789
0 abcdef
  
Press any key to continue
// Istream & Getline (char *, int N, char delimit = '\ n ');
// Getline extracts the terminator from the stream. The default Terminator is carriage return.
# Include <iostream. h>
Void main ()
{
Char S1 [10], S2 [10], S3 [10];
Cin. Getline (S1, 10 );
Cin. Getline (S2, 10 );
Cin. Getline (S3, 10 );
Cout <"------------------- \ n ";
Cout <S1 <Endl;
Cout <S2 <Endl;
Cout <S3 <Endl;
}
1234567890 abcdef
Bye bye
---------------------
123456789
0 abcdef
Bye bye
Press any key to continue
// Isrream & read (char *, INT );
# Include <iostream. h>
Void main ()
{
Char C [10];
Cin. Read (C, 10 );
Cout. Write (C, Cin. gcount () <Endl;
}
Running result:
1234567890 abcdef
1234567890
Press any key to continue

2. I/O format Control
The IOS class has some format control flags. You can use public functions to change these flags.
Long flags (); // returns the flag value.
Long flags (long); // set the flag value
Long SETF (long); // you can specify a flag.
Long SETF (Long, long); // you can specify the alignment, number, and floating point flag.
Long unsetf (long); // clear the flag
Int width ();
Int width (INT); // No persistence, effective for data output immediately after
Char fill ();
Char fill (Chac); // fill character
Int precision ();
Int precision (INT); // sets the precision.

When setting a flag, you can use the enumerated constant defined by the IOS class:
Enum {skipws = 0x0001,
Left = 0x0002, // adjustfield
Right = 0x0004,
Internal = 0x0008,
Dec = 0x0010, // basefield
Oct = 0x0020,
Hex = 0 x0040,
Showbase = 0x0080,
Show point = 0x0100,
Upperexample = 0x0200,
Showpos = 0x0400,
Scientific = 0x0800, // floatfield
Fixed = 0x1000,
Unitbuf = 0x2000,
Stdio = 0X4000
};
                
Use member function control:
# Include <stdio. h>
# Include <iostream. h>
Void main ()
{
Int I = 234;
Printf ("% 08x \ n", I );
Cout. SETF (IOs: Hex, IOS: basefield );
Cout. SETF (IOs: uppercase );
Cout. Width (8 );
Cout. Fill ('0 ');
Cout <I <Endl;
Cout. SETF (IOs: Dec, IOS: basefield );
Cout <I <Endl;
}
Running result:
000000ea
000000ea
234
Press any key to continue

Use stream operator to control the format
An operator can be directly used as an I/O output expression. Operators are classified into non-parameter operators and parameter operators.
Operators are classified into non-parameter operators iostream. h
Dec // decimal
Oct // octal
Hex // hex
Endl // line feed
Flush // output immediately
Iomanip. h
Resetiosflags (long) // clear the flag
Setfill (INT) // set the padding character
Setiosflags (long) // sets the flag.
Setprecision (INT) // sets the precision
SETW (INT) // set the width
// Use stream operator to control the format
# Include <iostream. h>
# Include <iomanip. h>
Void main ()
{
Int I = 234;
Cout Cout <I <Endl;
Cout <dec <I <Endl;
Cout <SETW (8) <I <Endl;
}
Running result:
000000ea
EA
234
00000234
Press any key to continue
Show floating point number
// If fixed or scientific is not specified, 6 Valid digits are output by default.
// After specified, the precision is the number of valid digits after the decimal point
# Include <iomanip. h>
# Include <stdio. h>
# Include <iostream. h>
Void main ()
{
Double D = 123.4567890123456789123456789;
Cout <D <Endl; // The default value is 6 Valid digits.
Cout <setprecision (4) <D <Endl; // retain the third digit after the decimal point
Cout <setiosflags (IOs: fixed) <D <Endl; // scientific notation
Cout <setprecision (20) <D <Endl; // can I display 20 digits?
Cout <resetiosflags (IOs: fixed); // cancel fixed point display
Cout <setiosflags (IOs: Scientific); // scientific notation
Cout <setprecision (4) <D <Endl; // retain the second decimal point
}
Running result:
123.457
123.5
123.4568
123.456789012345680
1.2127e + 002
Press any key to continue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.