Вы находитесь на странице: 1из 16

30.

C++

1.
2. - ++
3. -
4. ++
5. . / / (-)
6.
7.

1

30. C++


,
. , ,
.
.
- , ,
.
:
- .

, , :
, , ,
, .

.
,
,
( ) ( ).
:
- , .
- , .
- , ,
.
, , -
,
. , ,

2

30. C++

-
. -
-
,
.
.

- ++
, ,
++,
iostream.
-,
.
-
.
: : iostream.h: stream - , "i" -
. input - , "o" - . output - .
iostream -,
. :
1. cin - istream,
( ).
2. cout - ostream,
( ).
3. cerr - ostream,
( ),
.
4. clog - , cerr, .
iostream
cin, cout, cerr clog, ..
,
-.

, iostream.
++
: istream ostream.
ostream
(<<),
.
istream
(>>),
.

, .. .
istream ostream
.

3

30. C++


, , .
/
.
:

istream& operator >> (istream&, _&);


ostream& operator << (ostream&, _&);

: / ,
,
.

, .
.

-
. , , , -
, . ++
fstream. , :
C .
C .
.
.
.
.
.
.
: fstream
using namespace std;
fstream, iostream ,
:
ofstream - () .
ifstream - () .
fstream - .
.
:

fstream() - , ;

fstream(

const char* name, //


int omode, //
int = filebuf::openprot //

) - , ;

4

30. C++

fstream(

int f //

) -

fstream(
int f, //
char *buf, //
int len //

) - , , .

omode.
:

enum _Openmode {
in = 0x01, //
out = 0x02, //
ate = 0x04, //
app = 0x08, //
trunc = 0x10, //
_Nocreate = 0x40, // ,
_Noreplace = 0x80, // ,
binary = 0x20 //
};

: ios::.
, ios::in.
.

void open(const char *fileName, int mode = __,int protection =


__);

.
fileName - .
, ,
.
mode - .
protection - .

.

int close();

5

30. C++

,
.
:
.

istream&amp; istream::read(unsigned char *buf, int len);


istream&amp; istream::read(signed char *buf, int len);

.
len - ,
buf.

ostream&amp; osteram::write(const unsigned char *buf, int n);


ostream&amp; osteram::write(const signed char *buf, int n);

.
n - , -,
buf.

int istream::get();
istream& istream::get(unsigned char&);
istream& istream::get(signed char&);

ostream& ostream::put(char);

istream& istream::get(unsigned char *buf, int n, char c = '\n');


istream& istream::get(signed char *buf, int n, char c = '\n');

.

-, n
, .
.

6

30. C++

istream& istream::getline(unsigned char *buf, int n, char c = '\n');


istream& istream::getline(signed char *buf, int n, char c = '\n');

, get,
( ).

istream& istream::ignore(int n = 1, int d = EOF);

,
d n .

int istream::gcount();

,
.

int istream::peek();

""
- ( EOF, ),
.
.

istream& istream::putback(char cc);

, cc,

.

istream& istream::seekg(long pos);

,
.

7

30. C++

istream& istream::seekg(long off, ios::seek_dir dir);


, dir,
enum seek_dir {beg, cur, end};.
( ) long off.
beg, ; cur -
; end - .

ostream& ostream::seekp(long pos);

ostream& ostream::seekp(long off, ios::seek_dir dir);

seekg(),
ostream .

long istream::tellg();

long ostream::tellp();

, , - .
.

8

30. C++


++
, . ,
, , ++,
.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string.h>
#include <conio.h>
using namespace std;

//
#define MAX_PATH 260
//
#define NUM_COLS 18
//
#define NUM_ROWS 24

void main()
{
char path[MAX_PATH];
//
cout << "Input path to file: ";
cin.getline(path, MAX_PATH);

int counter = 0, i = 0, j = 0;
char text[NUM_COLS];

//
ifstream input(path, ios::in | ios::binary);
if (!input)
{
cout << "Cannot open file for display!" << endl;
return;
}

//
cout.setf(ios::uppercase);

// ,
//
while (!input.eof())
{
//
for (i = 0; (i < NUM_COLS && !input.eof()); i++)
input.get(text[i]);

if (i < NUM_COLS)
i--;

for (j = 0; j < i; j++)


if((unsigned)text[j] < 0x10)

// ?
cout << setw(2) << 0 << hex << (unsigned) text[j];

else
cout << setw(3) << hex << (unsigned) text[j];

9

30. C++

//
for (; j < NUM_COLS; j++)
cout << " ";

cout << "\t";


for (j = 0; j < i; j++)
// ?
if(text[j] > 31 && text[j] <= 255)
cout << text[j];
else
cout << ".";
cout << "\n";

// ,
if (++counter == NUM_ROWS)
{

counter = 0;
cout << "Press any key to continue..." << flush;

//
getch();
cout << "\n";

}
}
//
input.close();
}

. /
/ (-)
,
. :
- (
).
- .
#include <windows.h>
#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;

void main()
{
char Answer;
const MessageCount = 8;
int i, j;

//
enum {CHOICE = 3, INPUT_FILENAME, INPUT_DIMENSIONS, INPUT_ELEMENTS,
FILE_ERROR};

//
char Msg[MessageCount][50] =
{
"1. \n",
"2. \n",

10

30. C++

"3. \n",
"\n : ",
" : ",
" :\n",
" :\n",
" \n"
};

//

for(i = 0; i < MessageCount; i++)


CharToOem(Msg[i], Msg[i]);

do
{
for(int i = 0; i < 4; i++)
cout << Msg[i];
cin >> Answer;
} while (Answer < '1' || Answer > '3');

if(Answer == '3')
return;

//
char FileName[80];

//
int M, N;

int num;
cout << "\n" << Msg[INPUT_FILENAME];
cin >> FileName;

// ,
//
if(Answer == '1')
{
// ,
//
ifstream inF(FileName, ios::in | ios::_Nocreate);
if (!inF)
{
cout << endl << Msg[FILE_ERROR];
return;
}
//
inF >> M;
inF >> N;
//
for (i = 0; i < M; i++)
{
for (j = 0; j < N; j++)
{
inF >> num;
cout << setw(6) << num;
}
cout << endl;
}
inF.close();
}

// ,
//
else
{

11

30. C++

// .
// ,
//
ofstream outF(FileName, ios::out);
if (!outF)
{
cout << "\n" << Msg[FILE_ERROR];
return;
}
//
cout << Msg[INPUT_DIMENSIONS];
cout << "M: ";
cin >> M;
cout << "N: ";
cin >> N;

outF << M << ' ' << N << "\n";

cout << Msg[INPUT_ELEMENTS];


//
for (i = 0; i < M; i++)
{
for(j = 0; j < N; j++)
{
cout << "A[" << i << "][" << j << "] = ";
cin >> num;
outF << num << " ";
}
outF << "\n";
}
outF.close();
}
}

, ,
.
, . ,
,
.
#include <iostream>
#include <fstream>
#include <string.h>
#include <windows.h>
using namespace std;

void RussianMessage(char *message){


char rmessage[256];
CharToOem(message,rmessage);
cout<<rmessage;
}

int RussianMenu(){
RussianMessage("\n 1
\n");
RussianMessage(" 2 \n");
RussianMessage(" 3 \n");

12

30. C++

int choice;
cin>>choice;
return choice;
}

class Man{
//
int age;
//
char *name;
//
char *surname;
public:
//
Man(char *n,char *s,int a);
//
Man();
//
~Man();
public:
//
void Put();
//
void Show();
//
void SaveToFile();
//
static void ShowFromFile();
};

//
Man::Man(){
name=0;
surname=0;
age=0;
}

//
Man::Man(char *n,char *s,int a){
name=new char[strlen(n)+1];
if(!name){
RussianMessage(" !!!");
exit(1);
}
strcpy(name,n);
surname=new char[strlen(s)+1];
if(!surname){
RussianMessage(" !!!");
exit(1);
}
strcpy(surname,s);

age=a;
}

//
Man::~Man(){
delete[] name;
delete[] surname;
}

//
void Man::Put(){
char temp[1024];
RussianMessage("\n :\n");

13

30. C++

cin>>temp;

if(name)
delete[] name;

name=new char[strlen(temp)+1];
if(!name){
RussianMessage(" !!!");
exit(1);
}
strcpy(name,temp);
RussianMessage("\n :\n");
cin>>temp;

if(surname)
delete[] surname;

surname=new char[strlen(temp)+1];
if(!surname){
RussianMessage(" !!!");
exit(1);
}
strcpy(surname,temp);

RussianMessage("\n \n");
cin>>age;
}

//
void Man::Show(){
RussianMessage("\n:\n");
cout<<name;
RussianMessage("\n:\n");
cout<<surname;
RussianMessage("\n:\n");
cout<<age<<"\n";
}

//
void Man::SaveToFile(){
int size;
fstream f("men.txt",ios::out|ios::binary|ios::app);
if(!f){
RussianMessage(" !!!");
exit(1);
}
//
f.write((char*)&age,sizeof(age));
size=strlen(name);
//
f.write((char*)&size,sizeof(int));
//
f.write((char*)name,size*sizeof(char));
size=strlen(surname);
//
f.write((char*)&size,sizeof(int));
//
f.write((char*)surname,size*sizeof(char));
f.close();
}

//
void Man::ShowFromFile(){
fstream f("men.txt",ios::in|ios::binary);
if(!f){

14

30. C++

RussianMessage(" !!!");
exit(1);
}
char *n,*s;
int a;
int temp;
//
while (f.read((char*)&a,sizeof(int))){

RussianMessage("\n:\n");

f.read((char*)&temp,sizeof(int));
n=new char[temp+1];
if(!n){
RussianMessage(" !!!");
exit(1);
}
f.read((char*)n,temp*sizeof(char));
n[temp]='\0';
cout<<n;

RussianMessage("\n:\n");
f.read((char*)&temp,sizeof(int));
s=new char[temp+1];
if(!s){
RussianMessage(" !!!");
exit(1);
}
f.read((char*)s,temp*sizeof(char));
s[temp]='\0';
cout<<s;

RussianMessage("\n:\n");
cout<<a<<"\n";
delete []n;
delete []s;
}
}

void main(){
Man *a;
//
do{
switch(RussianMenu()){
case 1: //
a=new Man;
a->Put();
a->SaveToFile();
delete a;
break;
case 2: //
Man::ShowFromFile();
break;
case 3://
RussianMessage(" \n");
return;
default: //
RussianMessage(" \n");
}

}while(1);

15

30. C++


:
1.
2.
3.
4.
5.
:





, , .

16

Вам также может понравиться