3.1. ++
C++ / , / /7/. , ostream, << write(). istream, >> get(). , /. , . , /. , . . , C++ fstream, ifstream ofstream . C++ fstream /. iostream.
:
- ofstream ;
- ;
- ofstream , cout; , , .
fstream. iostream C++, iostream . ofstream. C++, , fout, outFile, cgate didi. fout :
Ofstream fout.
. , n(). , , , cookies. :
// fout text.txt
fout.open("text.txt");
( ) , :
// fout text.txt
ofstream fout("text.txt");
fout ( ) , cout. , My name is Guzel , :
fout << "My name is Guzel";
, ostream ofstream, () ostream, , . ofstream , ofstream, fout. ofstream, . ofstream fout , . , , . , , .
|
|
, . , .
:
1. ifstream .
2. .
3. , cin.
. fstream, ifstream . :
//
// ifstream fin
ifstream fin;
// text.txt
fin.open("text.txt");
//
// fin text.txt
ifstream fin("text.txt");
fin , cin. , :
char ch;
// text.txt
fin >> ch;
char buf[80];
//
fin >> buf;
//
fin.getline(buf, 80);
, , . , , close():
//
fout.close();
//
fin.close();
, , . . , fin , . .
. . , . , . . , fin fout , cin cout.
, . , . , , . , , , ifstream ofstream . , , , 20.
|
|
. , , . , . , . n() . , :
//
ifstream fin;
// file1.dat
fin.open("file1.dat");
//
....
// file1.dat
fin.close();
// fin ( )
fin.clear();
// file2.dat
fin.open("file2.dat");
//
....
fin.close();
, , .. , n(), , :
//
ifstream fin("banjo", mode1);
ofstream fout();
// open()
fout.open("harp", mode2);
ios_base , . 1 . / C++ , / ANSI .
1.
ios_base::in | . |
ios_base::out | . |
1 | |
ios_base::ate | . |
ios_base::app | . |
ios_base::trunc | , . |
ios_base::binary | . |
, ios_base::trunc , ; , . . , . C++, , . , , , ios_base::app:
ofstream fout("text.txt", ios_base::out | ios_base::app);
| . , ios_base::out | ios_base::app , out, .
, , , . , .
C++ / C++ ANSI . C++:
ifstream fin(filename, c++mode);7
, fopen():
fopen(filename, cmode);
c++mode openmode, ios_base::in ANSI , a cmode , , "r". 2 C++ . , ios_base::out , , ios_base::in. is_open().
|
|
2. C++
C++ | |
. | |
ios_base::in | "r" |
( , ios_base::out ios_base::trunc). | |
ios_base::out | "w" |
, , . | |
los_base::out | ios_base::trunc | "w" |
, . | |
ios_base::out | ios_base::app | "a" |
, . | |
ios_base::in | ios_base::out | "r+" |
, , . | |
ios_base::in | ios_base::out | ios_base::trunc | "w+" |
c++mode cmode, ; , ios_base::in | ios_base::binary "rb". | |
c++mode | ios_base::binary | "cmodeb" |
. . , ios_base::in | ios_base::ate fseek(file, 0, SEEK_ENP). | |
c++mode | ios_base::ate | "cmode" |
, ios_base::ate, ios_base::app . , ios_base::app , ios_base::ate .