Академический Документы
Профессиональный Документы
Культура Документы
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++
, , .
/
.
:
: / ,
,
.
, .
.
-
. , , , -
, . ++
fstream. , :
C .
C .
.
.
.
.
.
.
: fstream
using namespace std;
fstream, iostream ,
:
ofstream - () .
ifstream - () .
fstream - .
.
:
fstream() - , ;
fstream(
) - , ;
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.
.
.
fileName - .
, ,
.
mode - .
protection - .
.
int close();
5
30. C++
,
.
:
.
.
len - ,
buf.
.
n - , -,
buf.
int istream::get();
istream& istream::get(unsigned char&);
istream& istream::get(signed char&);
ostream& ostream::put(char);
.
-, n
, .
.
6
30. C++
, get,
( ).
,
d n .
int istream::gcount();
,
.
int istream::peek();
""
- ( EOF, ),
.
.
, cc,
.
,
.
7
30. C++
, dir,
enum seek_dir {beg, cur, end};.
( ) long off.
beg, ; cur -
; end - .
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--;
// ?
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 << " ";
// ,
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"
};
//
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;
, ,
.
, . ,
,
.
#include <iostream>
#include <fstream>
#include <string.h>
#include <windows.h>
using namespace std;
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