.


:




:

































 

 

 

 





++ . , , , ; ++.

.

, .

.

(, , ).

.

.

- .

.

.

 


 

, .

, . , . , , .

( ) ( , ).

, , , .

.

.

.

++ , ios sterambuf. ios , steambuf .

:

ios ;

istream ;

ostream ;

iostream ;

istringstream ;

ostringstream ;

stringstream ;

ifstream ;

ofstream ;

fstream .

:

<ios> /;

<iosfwd> /;

<istream> ;

<ostream> ;

<iostream> /;

<fstream> / ;

<sstream> / ;

<streambuf> /;

<iomanip> (. ).

 

<iostream> / (.1).

 

1

 

     
cin istream ( )
cout ostream ( )

.1

     
cerr ostream ( ),
clog ostream ( ),

 

 

istream ostream >> << .

( 0x) . ( ).

( 0-).

.

 

: , .

, x_flags long ios.

 

2

 

     
skipws +
left -
right +
internal - ,
dec +
oct -
hex -
showbase - (0x , 0 )
showpoint -
uppercase -
scientific -
fixed -

 

ios flags, setf unsetf.

long ios:: flags (long); ;

long ios:: flags (long); ;

long ios:: setf (long, long); , , ;

long ios:: setf (long, long); , ;

long ios:: unsetf (long, long); , ;

.

ios:

int x_width ;

int x_precision ;

int x_fill .

width, precision fill:

int ios:: width () ;

int ios:: width () ;

int ios:: precision () ;

int ios:: precision (int) ;

char fill () ;

char fill (char) ( ).

, . setf ios:

adjustfield (left | right | internal)

basefield (dec | oct | hex)

floatfield (scientific | fixed)

# include <iostream.h>

int main () {

long a = 100, b = 077;

cout.width (7); cout.unsetf (ios:: dec);

cout.setf (ios:: hex | ios:: showbase | ios::uppercase);

cout << a;

cout.width (7);

cout << b << endl;

double d = 0.12, c = 1.3e-4;

cout.setf(ios:: left);

cout << d << endl;

cout << c;

return 0;

}

 

:

0X3T8 0X3F

0.12

0.00013

, . . .

dec ;

oct ;

hex ;

ws ;

endl ;

ends ;

flush .

.

cout << 13 << hex << << 13 << oct << << 13 << endl;

:

13 d 15

, . <iomanip>.

setbase (int n) (n = 8, 10, 16 0). 0 (, , );

resetiosflags (long) , ;

setfill (int) - , ;

setprecision (int) ;

setw (int) ;

setiosflags (long) , 1.

 

double d (1.234);

cout << setfill (.) << setprecision (4)

<< setiosflags (ios:: showpoint | ios:: fixed);

cout << setw (12) << d << endl;

:

1.2340

 

 

>> << ( ).

 

   
istream
gcount () ,
get () EOF
get (buf, num, lim = \n) num-1 ( lim) buf. lim (\0). lim .
getline (buf, num, lim = \n) get (), buf lim    

   
ignore (num = 1, lim = EOF) , num ( 1) lim.
peek () EOF,
putback (c) ,
read (buf, num) num ( , num) buf
readsome (buf, num) num buf
seekg (pos) pos
seekg (offs, org) offs , , org: ios:: beg ( ), ios:: cur ( ) ios:: end ( )
tellg ()
ostream
flush ()
ut ()

   
seekg (pos) pos
seek (ofs, org) offs , (. )
tellg ()
write (buf, num) num buf

1. .

# include <iostream.h>

int main () {

const int N = 20, Len = 100;

char str [Len] [N]

int i = 0;

while (cin.getline (str [i], Len, \n) & & i < N) {

i ++;}

return 0;

}

2. test, ( . ).

# include <iostream.h>

# include <fstream.h>

# include <string.h>

int main () {

//

ofstream out (test);

if (!out) {

cout << << endl;

return 1;

}

char *str [ ] = {the first line, the second line, the third line};

for (int i = 0; i<3, ++ i) {

out.write (str [i], strlen (str [i]));

out.put (\n);

}

out.close ();

//

ifstream in (test);

if (! in) {

cout << << endl;

return 1;

}

char check_str [3] [30];

for (int i = 0; i < 3; ++ i) {

in.get (check_str [i], 30);

in.get (); }

for (int i = 0; i < 3; ++ i) cout << check_str [i] << endl;

in.close ();

return 0;

}

3. peek () putback ().

, . , . .

# include <iostream.h>

# include <fstream.h>

# include <stdlib>

int main () {

char ch;

//

ofstream out (test);

cout << test << endl;

return1;

}

char str [80], *p;

out << 123 << << 23;

out << << 99 << << endl;

out.close ();

//

ifstream in (test);

if (!in) {

cout << test << endl;

return 1;

}

do {

p = str;

ch = in.peek (); //

if (isdigit(ch)) {

while (isalpha (*p = in.get ())) p ++; //

in.putback (*p); //

* = \0; //

cout << : << atoi (str);

}

else if (isalpha (ch)) { //

while (isalpha (*p = in.get ())) p ++;

in.putback (*p); //

*p = \0; //

cout << : << str;

}

else in.get (); //

cout << endl;

} while (!in.eof ());

in.close ();

return 0;

}

:

: 123

:

:

:

: 23

:

:

: 99

:

ios state, :

enum io_state {

googbit = 0x00, //

eofbit = 0x01, //

failbit = 0x02, //

badbit = 0x04, // ,

//

hardbit = 0x08 //

};

:

int rdstate () ;

int eof () , eofbit;

int fail () , failbit, badbit hardbit;

int good () , ;

void clear (int = 0) , 0;

operator void *() , ;

operator! () , .

. :

// , flag

if (steam_obj.rdstate () & ios:: flag)

// flag

stream_obj.clear (rdstate () & ~ios:: flag)

// flag

stream_obj.clear (rdstate () | ios:: flag)

// flag

stream_obj.clear (ios:: flag)

//

stream_obj.clear ()

 

void *() , . :

while (stream_obj) {

// , /

}

 

:

ifstream ;

ofstream ;

fstream .

istream, ostream iostream .

:

- ;

- ;

- (/);

- ;

- .

, .

, :

ifstream ();

ofstream ();

fstream ();

, :

ifstream (const char * name, int mode = ios:: in);

ofstream (const char * name, int mode = ios:: out | ios:: trunc);

fstream (const char * name, int mode = ios:: in | ios:: out);

.

 

 

, open, , .

1

ifstream inpf (input.txt); //

if (! inpf) {

cout << ;

return 1;

}

2

ofstream f;

f.open (output.txt, ios:: out); // open

if (!f) {

cout << ;

return 1;

}

, , .

. .

# include <fstream.h>

int main () {

char text [81], buf [81];

cout << ;

cin >> text;

ifstream f (text);

if (!f) {

cout << ;

return 1;

}

while (!f.eof ()) {

f.getline (buf,81);

cout << buf << endl;

}

return 0;

}

close (), , , .

-.

, , . :

istringstream ;

ostringstream ;

stringstream .

<sstream> istream, ostream iostream .

, , ++ ( string). . :

explicit istringstream (int mode = ios:: in);

explicit istringstream (const string & name, int mode = ios:: in);

explicit ostringstream (int mode = ios:: out);

explicit ostringstream (const string & name, int mode = ios:: out);

explicit stringstream (int mode = ios:: in | ios:: out);

explicit stringstream (const string & name, int mode = ios:: in | ios:: out);

explicit , .

str, :

string str () const;

void str (const string & S);

. , :

# include <sstream>

# include <string>

# include <iostream>

# include <ctime>

using namespace std;

string message (int i) {

ostringstream os;

time_t t;

time (&t); // time_t

os << time: << ctime (&t) <<number: << i << endl;

return os.str (); // ctime ()

//

}

int main () {

cout << message (22);

return 0;

}


 

 

++ . , -. , <string.h>.

() string , . , . <string>.





:


: 2016-10-06; !; : 344 |


:

:

.
==> ...

1397 - | 1250 -


© 2015-2024 lektsii.org - -

: 0.183 .