, , - . :
- , / ;
- () / ;
- , , , /.
( ), . . , ? ++ ߹ , :
Namespace <__ ()> { ߹ }
, :
using namespace:: <__>;
. ++ :
using namespace::std;
. 8 .
8
#include <vcl.h>
#include <iostream>
#include <conio.h>
namespace F {
float x = 9;
}
namespace G {
using namespace F; /* G F, : INNER_G */
float y = 2.0;
namespace INNER_G {
float z = 10.01;
}
} // G
int main() {
using namespace G; /* ߹ , G */
using namespace G::INNER_G; /* ߹ , INNER_G */
float x = 19.1; //
std::cout << "x = " << x << std::endl;
std::cout << "y = " << y << std::endl; // y F
std::cout << "z = " << z << std::endl; /* z INNER_G */
getch();
return 0;
}
:
x = 19.1
y = 2
z = 10.01
std::cout . . , ' cout std.
fstream
, , (, , ) , .
():
- open () - ;
- close () - ;
- is_open () - , true, - false;
- rdbuf () - /.
open ():
open (char * file_name, open_mode);
|
|
file_name - ' , open_mode - .
:
enum open_mode {app, binary, in, out, trunc, ate};
ios, fstream, , -: ios:: app, ios:: binary . .
:
- app - ;
- binary - ( );
- in - ;
- out - . , ;
- trunc - , ( );
- ate - .
(||), .
fstream ( 9).
. . 2.
9
#include <vcl.h>
#include<fstream>
#include<iostream>
#include <conio.h>
#include <stdio.h>
void main() {
using namespace std; /* . (- ) ' () */
fstream inout;
inout.open ("fstream.out", ios_base:: in | ios_base:: out |
ios_base:: trunc);
//
inout << "This is the story1 of a man" << endl;
inout << "This is the story2 of a man" << endl;
inout << "This is the story3 of a man" << endl;
char p [100];
// () inout.seekg (0);
// 1- ( 100 )
inout.getline (p, 100);
// 1- (stdout)
cout << endl << "String1:" << endl;
cout << p;
// ' 1-
fstream:: pos_type pos = inout.tellg ();
// 2-
inout.getline (p, 100);
// 2- (stdout)
cout << endl << "String2:" << endl;
cout << p;
// 3-
inout.getline (p, 100);
// 3- (stdout)
cout << endl << "String3:" << endl;
cout << p;
// 2-
inout.seekp (pos);
// 2-
inout << "This is the story2 of a man" << endl;
// 3-
inout << "This is the story3 of a man" << endl;
//
inout.seekg (0);
// (stdout)
cout << endl << endl << inout.rdbuf ();
inout.close ();
system ("DEL FSTREAM.OUT");
getch ();
}
. 2. 9
ofstream
ofstream () :
r open () - ;
|
|
r is_open () - true, , false - ;
r put () - ;
r write () - ;
r skeep () -
;
r tellp () - ;
r close () - ;
r rdbuf () - ( , ' ).
10 ofstream.
10
ofstream FILE; /* FILE ofstream
( ) */
FILE.open ("a.txt"); //
if (FILE == NULL) return (0); //
for (int i = 0; i <2; i + +)
FILE << "string" << i << endl; //
FILE.close (); //
ifstream
ifstream () :
- open () - ;
- is_open () - true, , false - ;
- get () - ;
- read () - ;
- eof () - , ;
- peek () - , ( );
- seekg () -
;
- tellg () - ;
- close () - ;
- rdbuf () - ( , ' ).
11.
11
ifstream FILE; /* FILE ifstream ( ) */
char p [100];
FILE.open ("a.txt"); //
if (FILE == NULL) return (0); //
while (! FILE.eof ()) // {
FILE >> p; //
cout << p << endl; //
}
FILE.close (); //
/ ( 12).
12
# Include <vcl.h>
# Include <iostream>
# Include <fstream>
# Include <conio.h>
# Define DelKey 's' / /
# Define maxline 1000
// ------------------------------------------------ -------------------- # pragma argsused
int main (int argc, char * argv []) {
using namespace std; //
//
ofstream FILE;
FILE.open ("c: \\ a.txt", ios:: out);
char p [maxline];
int i, pos;
for (i = 0; i <2; i + +)
FILE << "string" << i; /* "<< endl" , , */
FILE.close ();
// ( )
ifstream FILE1;
FILE1.open ("c: \ \ a.txt");
FILE1.seekg (0); /* ( , seekg ()) */
if (FILE1 == NULL) / /
return (0);
while (! FILE1.eof ()) // {
FILE1 >> p >> i;
cout << p << i << endl;
}
FILE1.close ();
getch ();
//
ifstream FILE2;
char c;
FILE2.open ("c: \ \ a.txt");
if (FILE2 == NULL) //
return (0);
while (! FILE2.eof ()) // {
c = FILE2.peek (); /* , : */
|
|
streampos cgp = FILE2.tellg (); /* */
if (c == DelKey) /* DelKey */ {
pos = cgp + 1; // seekg ()
FILE2.seekg (pos); /* , */
continue; //
}
FILE2.get (c); //
cout << c;
} // While
cout << endl;
FILE2.close ();
getch ();
system ("DEL C: \ \ A.TXT"); //
} // Main ()
, , . 13.
13
# include <vcl.h>
# include <iostream> / / cin, cout
# include <fstream>
# include <conio.h>
# include <stdio.h>
void main () {
using namespace std; //
/* */
struct Blocknotes {
char name [30];
char phone [15];
int age;
} B [2] = {
"
}; //
//
ofstream FILE;
FILE.open ("Block", ios:: binary);
for (int i = 0; i <2; i++)
FILE.write ((char *) & b [i], sizeof (b [i]));
FILE.close ();
//
ifstream FILE1;
FILE1.open ("Block", ios:: binary);
Blocknotes bb [2];
int i = 0;
while (! FILE1.eof ()) {
if (i == 2)
goto m;
FILE1.read ((char *) & bb [i], sizeof (bb [i]));
cout << "string" << i << "" << bb [i]. name << ""
<< Bb [i]. Phone << "" << bb [i]. Age << endl;
i++;
}
m:
FILE1.close ();
system ("DEL BLOCK");
getch ();
}
:
-
FILE.write((char *)&b[i], sizeof(b[i]));
write (), , . , & b [i]. , char ( ). - . sizeof ();
- system ("DEL BLOCK") ;
- goto bb [].
. 3.
. 3. 13
/ ++
/ /. / << >>. , istream, ostream, , '- cout ( ), cin ( ) err ( ). - ++ () - (cin), - (cout cerr). ' stdin, stdout, stderr. / , , , : cin, cout. , <<, >> . , , / . , :
|
|
cout << i;
i , i .
:
cin >> i >> j >> s;
i, j, s int, float, char. , ( <Enter>) .
cout
' cout ߹ -, ' ' stdout, stdio.h. ++ .
- . . 4.
, "no" (noshowpos . .) , "no". "" .
4. / ++
- | ||
showpos | setf (ios:: showpos) | |
noshowpos | unsetf (ios:: showpos) | - |
showbase | setf (ios:: showbase) | |
noshowbase | unsetf (ios:: showbase) | - |
uppercase | setf (ios:: uppercase) | |
nouppercase | unsetf (ios:: uppercase) | - |
showpoint | setf (ios:: showpoint) | ( ) |
noshowpoint | unsetf (ios:: showpoint) | - |
boolalpha | setf (ios:: boolalpha) | |
noboolalpha | unsetf (ios:: boolalpha) | - |
unitbuf | setf (ios:: unitbuf) | |
nounitbuf | unsetf (ios:: unitbuf) | - |
internal | setf (ios:: internal, ios:: adjustfield) | - ( ). , |
left | setf (ios:: left, ios:: adjustfield) | - ( ) |
right | setf (ios:: right, ios:: adjustfield) | - ( ) |
dec | setf (ios:: dec, ios:: basefield) | ( ) |
hex | setf (ios:: hex, ios:: basefield) | ( ) |
oct | setf (ios:: oct, ios:: basefield) | ( ) |
fixed | setf (ios:: fixed, ios:: floatfield) | |
scientific | setf (ios:: scientific, ios:: floatfield) | , : , 23450000 : 23.45e6 |
setbase (int base) | base, base 8, 10 16 | |
fill (c) | setfill (char_type c) | |
precision (n) | setprecision (int n) | ( ) |
setw (int n) | width (n) | ( ) |
endl | ߹ ('\n') | |
ends | ߹ '\0' | |
flush | flush () | ws |
:
- precision () - 6;
- width () - 0;
- fill () - .
' cout ( 14).
|
|
. . 4.
14
// cout example
# Include <vcl.h>
# Include <iostream>
# Include <iomanip> //
# Include <conio.h>
void main () {
using namespace std;
int i;
float f;
cout << "Enter i and f>" << endl;
// stdin
cin >> i >> f;
//
cout << i << endl;
//
cout << f << endl;
//
cout << hex << i << endl;
//
cout << oct << i << dec << i << endl;
// i
cout << showpos << i << endl;
// i
cout << setbase (16) << i << endl;
/* i @ 20 ( ). 45, , 45 @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ */
cout << setfill ('@') << setw (20) << left << dec << i;
cout << endl;
// ,
//
cout.fill ('@');
cout.width (20);
cout.setf (ios:: left, ios:: adjustfield);
cout.setf (ios:: dec, ios:: basefield);
cout << i << endl;
// f - 10
cout << scientific << setprecision (10) << f << endl;
// 6
cout.precision (6);
// f
cout << f << fixed << endl;
getch ();
}
. 4. 14
cin
' ( ) cin ߹ , ' ' stdin, stdio.h. ++ . , cout. , dec, hex, oct, ws .
' cin 15.
15
// Cin example # 1
# include <vcl.h>
# include <fstream>
# include <iostream>
# include <conio.h>
void main () {
using namespace std;
int i;
float f;
char c;
// , stdin
cout << "Enter i, f, c and then input the string>" << endl;
cin >> i >> f >> c;
// i, f c stdout
cout << i << endl << f << endl << c << endl;
//
// Cin example # 2
//
char p [50];
//
cin >> ws >> p;
cout << p << endl;
// stdin, <Enter>
// 49
cin.seekg (0);
cin.getline (p, 50);
// stdout
cout << p << endl;
getch ();
}
. 5.
. 5. 15