Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Examination_IT / Programming bases on With C++

.pdf
Скачиваний:
17
Добавлен:
16.05.2015
Размер:
7.43 Mб
Скачать

241

=

;@:

1)r ofstream, ifstream

fstream (Z ,

(r) );

2)=, ; = ; open, -

d =;

3)= @ ; = write read;

4)= @ ; = close.

10.1. I 8.4012 A B?2

D =

d = -

. ?, =-

=

, ,

. ? =-

-

;@ ,

, @,

, =. . -

, = ,

;. B, =

,

@ = d : -

, ; . : Z = ,

, . B

=.

? =, @ d -, , , - -

242

d. t, , = -

d txt. (. ; d cpp, cs, pas, cfg, html, log, mod ). 3 =

, , Aditor, EmEditor

. . 5 , = -

1, -

ANSI ( . 6.4). 3, -

«Bllo», = @ Z -

= (72, 101, 108, 108, 111) ;@

«Bllo» ( . 1). !, ;

@ ;, ;

. ? Z -

; ,

1 , Z .

F

H

e

l

l

o

 

72

101

108

108

111

ANSI

 

 

 

 

 

 

01001000

01100101

011001100

011001100

011001111

ANSI

 

 

 

 

 

? , -

, ; …

«Hello». ? = Z -

. B ,

; = =-

;. , = -

13436, @ ;

13436 0011010001111011 ,

;@ 2 . 3

;@

ANSI ( . 1), :

1 B , « »,

.

243

4

1

3

4

3

6

 

49

51

52

51

54

ANSI

 

 

 

 

 

 

00110001

00110011

00110100

00110011

00110110

ANSI

 

 

 

 

 

3 , :

0011010001111011

(1)

 

; =, -

- ;@ ;

(=):

0011000100110011001101000011001100110110 (2)

~ (1) 2 , - = (2) 5 ,

13436. ~@ =-

@ , -

= ;. : Z

, =

=, ,

. ? = -

. . = ( . 10.1)

, . 5 .

244

t 13436 « » =

t 13436

 

 

 

 

 

0011000100110011001101000011001100110110

 

0011010001111011

`

 

 

 

 

 

 

 

 

 

 

 

ofstream

 

cout

 

.

 

 

 

 

 

 

 

 

 

.

13436

.

.

˜

`

D. 10.1. F =

W !

s = = -

= , Z

d .

? = :

iNumber @ iN Z,

@ fNumber, @ fN Z. !-Z, = , -

@ text1 text2.

F ;@ 10.1. e, d , . 10.2 -

. : , =

TestFile.txt d .

8 58 9 10.1. !6:; Ex10_01.cpp

#include "stdafx.h"

 

245

#include <iostream>

// AQRQPQ<DK <<Q^-<D<Q^.

#include <locale.h>

// ^=; setlocale.

#include <iomanip>

// <P=mJ>?@> CH?@Ab=;RQBQ<.

#include <fstream>

// ^=; ofstream.

using namespace std;

 

////////////////////////////////////////////////////////

// t=H<?H; ab?Pc@;. void main()

{

setlocale(LC_CTYPE, "Russian_Russia.1251"); const double PI = 3.1415926535897932;

char text1[] = "•>=D> J@F=H:";

// A>B<DK

 

 

// IHdQ=Q<QP.

char text2[] = "‚>{>FR<>??D> J@F=H:"; // <RQBQK

const int iN = 5;

 

// IHdQ=Q<QP.

// PQ=@J>FR<Q c>=DE J@F>=.

int iNumber[iN] = {3, -998, 73, 456, 56};

const int fN = 4;

// PQ=@J>FR<Q <>{>FR<>??DE

 

// J@F>=.

 

double fNumber[fN] = {PI/3.0, PI*345.0, -1.0*PI/56777.0, -1.0*PI*0.567};

char NameFile[] = "TestFile.txt"; // @C; aHK=H.

ofstream file; // FQI^H>C Q9:>PR R@AH aHK=. // 8RPBD<H>C aHK= F @C>?>C NameFile.

file.open(NameFile);

// QRPBD<H>C aHK=.

if(!file.is_open())

// >F=@ Qw@9PH.

{

 

cout << "8w@9PH QRPBDR@; aHK=H \"" << NameFile; cout << "\"" << endl;

cout << "GBQdBHCCH BH9QRb IHPQ?J@=H." << endl; system("pause");

return;

}

// G@w>C @?aQBCHc@m < aHK=.

file << text1 << endl;

// A>B<DK IHdQ=Q<QP.

file << iN << endl;

// A@w>C PQ=@J>FR<Q c>=DE

 

// J@F>=.

for(int i=0; i<iN; i++)

file << setw(5) << iNumber[i]; file << endl;

 

246

file << text2 << endl;

// <RQBQK IHdQ=Q<QP.

file << fN << endl;

// A@w>C PQ=@J>FR<Q

 

// <>{>FR<>??DE J@F>=.

for(int i=0; i<fN; i++)

file << setprecision(8) << scientific << fNumber[i] << endl;

file.close(); // IHPBD<H>C aHK=.

cout << "~H??D> IHA@FH?D < R>PFRQ<DK aHK=: " << NameFile;

cout << endl;

// †^>C ?HkHR@; =m9QK P=H<@w@ P=H<@HRbBD. system("pause");

}

D. 10.2. e TestFile.txt, -

Ex10_01

.

X ;

t = . ?

= ;

Ex10_01 = ( 10.1) ; = ;

247

. 3 10.2,

– . 10.3.

?

= . 3 Z -

, Z d =,

-

,

, = -

. . , d ,

=, Z d, -

,

.

:@… . ,

= ; getline. 5

,

. ?

= Ex10_01\TestFile.txt ( -

10.2) Ex10_02. 5

=.

8 58 9 10.2. !6:; Ex10_02.cpp

#include "stdafx.h"

// AQRQPQ<DK <<Q^-<D<Q^.

#include <iostream>

#include <locale.h>

// ^=; setlocale.

#include <iomanip>

// <P=mJ>?@> CH?@Ab=;RQBQ<.

#include <fstream>

// ^=; ifstream.

#define LIMTEXT 255

 

using namespace std;

 

////////////////////////////////////////////////////////

// t=H<?H; ab?Pc@;. void main()

{

setlocale(LC_CTYPE, "Russian_Russia.1251"); char buffer[LIMTEXT];

int Len;

char *text1; // A>B<DK IHdQ=Q<QP. char *text2; // <RQBQK IHdQ=Q<QP.

int iN; // PQ=@J>FR<Q c>=DE J@F>=.

 

 

248

int

*iNumber;

int

fN;

// PQ=@J>FR<Q <>{>FR<>??DE J@F>=.

double *fNumber;

char

NameFile[] = "TestFile.txt"; // @C; aHK=H.

ifstream file; // FQI^H>C Q9:>PR R@AH aHK=. // 8RPBD<H>C aHK= F @C>?>C NameFile.

file.open(NameFile);

// QRPBD<H>C aHK=.

if(!file.is_open())

// >F=@ Qw@9PH.

{

 

cout << "8w@9PH QRPBDR@; aHK=H \"" << NameFile; cout << "\"" << endl;

cout << "GBQdBHCCH BH9QRb IHPQ?J@=H." << endl; system("pause");

return;

}

// ‘@RH>C A>B<DK IHdQ=Q<QP < 9Q=fwQK 9ba>B. file.getline(buffer, LIMTEXT-1); // A>B<DK

// IHdQ=Q<QP.

Len = strlen(buffer); text1 = new char[Len+1];

strcpy_s(text1, Len+1, buffer);

//‘@RH>C PQ=@J>FR<Q c>=DE J@F>=. file >> iN;

iNumber = new int[iN]; for(int i=0; i<iN; i++) file >> iNumber[i];

//‘@RH>C <RQBQK IHdQ=Q<QP < 9Q=fwQK 9ba>B. file.getline(buffer, LIMTEXT);// J@RH>C F@C<Q= ?Q<QK

// FRBQP@. file.getline(buffer, LIMTEXT);// <RQBQK IHdQ=Q<QP. Len = strlen(buffer);

text2 = new char[Len+1]; strcpy_s(text2, Len+1, buffer);

// ‘@RH>C PQ=@J>FR<Q <>{>FR<>??DE J@F>=. file >> fN;

fNumber = new double[fN]; for(int i=0; i<fN; i++)

file >> fNumber[i];

cout << "~H??D>, ABQJ@RH??D> @I R>PFRQ<QdQ aHK=H "

249

<< NameFile << ":" ; cout << endl;

cout << text1 << endl;

cout << "]Q=@J>FR<Q c>=DE J@F>=: " << iN << endl; for(int i=0; i<iN; i++)

cout << setw(5) << iNumber[i]; cout << endl;

cout << text2 << endl;

cout << "]Q=@J>FR<Q <>{>FR<>??DE J@F>=: " << fN << endl;

for(int i=0; i<fN; i++)

cout << setprecision(10) << scientific << fNumber[i] << endl;

file.close(); // IHPBD<H>C aHK=.

//8F<Q9Qk^H>C AHC;Rf. delete text1;

delete text2; delete iNumber; delete fNumber;

//†^>C ?HkHR@; =m9QK P=H<@w@ P=H<@HRbBD. system("pause");

}

D. 10.3. D Ex10_02

250

10.2. `9/ /2 A B?2

= @ , -

= … ; d -

. ` ;. 9,

= /s. ? =

. F Z ; -

, «;» = =. e = .

=. 3, , -

Word, =-. 5 . ! -

, d =,

, ; ; = ;, ;

=. ? Z = @ ; ;- @ . / = -

. . =

, = ( , - =, =) .

W ! 6

= = = write ostream. ? =

ostream& write(const char* buff, int num);

? buff Z = c -

. ? num = -

.

?

=.

s c char =:

char c = 'A';

Соседние файлы в папке Examination_IT