.


:




:

































 

 

 

 


-




:

- , , ;

- ;

- - .

 

seekg() seekp().

, , . fstream, -, , .

tellg() tellp(). , .

 

.

 

11.3

 

#include "stdafx.h"

#include <iostream>

#include <fstream>

#include <string>

 

using namespace std;

 

int main()

{

setlocale(LC_ALL, "Russian");

 

const int len = 81;

char word[len], line[len], end_word[] = "done";

 

// /

fstream f("lab11.txt", ios::in | ios::out | ios::trunc);

if (!f)

{

cout << " " << endl;

return -1;

}

 

//

do

{

cin >> line;

// cin cout

f << line << endl;

}

// done

while (strcmp(line, end_word));

 

//

f.seekg(0);

 

cout << " : " << endl;

 

//

//

while (!f.eof())

{

f >> line;

cout << line << endl;

}

 

 

cout << " : ";

cin >> word;

 

// - eofbit

f.clear();

 

//

f.seekg(0);

 

//

size_t l_word = strlen(word);

 

//

int wc = 0;

// word

while (f.getline(line, len))

{

// p

char *p = line;

//strstr

// p word

while (p = strstr(p, word))

{

//

char *c = p;

 

// p

p += l_word;

 

//

// ()

 

//

if (c!= line)

//

//

if (!ispunct(*(c-1)) &&!isspace(*(c-1)))

//

// =>

continue;

 

//

//

// ,

// =>

if (ispunct(*p) || isspace(*p) || (*p == '\0'))

{

wc++;

cout << " " <<endl;

}

}

}

 

cout << " : "<< wc << endl;

 

return 0;

}

 

. C++ strtok, . . , , strtok. .

 

11.4 strtok

 

#include "stdafx.h"

#include <iostream>

#include <fstream>

#include <string>

 

using namespace std;

 

int main()

{

setlocale(LC_ALL, "Russian");

 

const int len = 81;

char word[len], line[len], end_word[] = "done";

 

// /

fstream f("lab11.txt", ios::in | ios::out | ios::trunc);

if (!f)

{

cout << " " << endl;

return -1;

}

 

//

do

{

cin >> line;

f << line << endl;

}

// done

while (strcmp(line, end_word));

 

cout << " : ";

cin >> word;

 

// - eofbit

f.clear();

//

f.seekg(0);

 

//

char delims[] = ",.!? /<>|()*:;\"";

 

//

char *token;

 

//

int wc = 0;

// word

while (f.getline(line, len))

{

//

//

token = strtok(line, delims);

 

//,

while (token!= NULL)

{

//strtok

// NULL

//

if (!strcmp(token, word))

wc++;

 

//

//strtok NULL

token = strtok(NULL, delims);

}

}

 

cout << " : "<< wc << endl;

 

return 0;

}

 

 

11.5

 

//

// ( , )

//

//

 

#include "stdafx.h"

#include <iostream>

#include <fstream>

#include <iomanip>

 

using namespace std;

 

int main()

{

setlocale(LC_ALL, "Russian");

 

const int NP = 10;

const int IS = sizeof(int); // int

 

int pt, i;

 

// /.

fstream fs("random.txt",

ios::binary | ios::in | ios::out | ios::trunc);

if (!fs)

{

cerr << " ." << endl;

return 1;

}

 

// .

cout << " :" << endl;

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

{

pt = i;

// pt char* write

fs.write((char*)&pt, IS);

cout << setw(4) << pt;

}

 

cout << endl << endl;

 

// .

cout << " :"<< endl;

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

{

// i-

fs.seekg(-(i + 1) * IS, ios::end);

// pt char* read

fs.read((char*)&pt, IS);

cout << setw(4)<< pt;

};

 

cout<< endl << endl;

 

// .

for (i=1; i<NP/2; i++)

{

// i-

fs.seekg(2 * i * IS);

// i-

fs.read((char*)&pt, IS);

//

pt = -pt;

// ,

int p = fs.tellg();

p -= IS;

fs.seekg(p);

//

fs.write((char*)&pt, IS);

}

 

//

cout << " :"<<endl;

fs.seekg(0);

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

{

fs.read((char*)&pt, IS);

cout << setw(4) << pt;

}

cout << endl;

 

fs.close();

return 0;

}

 

. , (.. ). , ios::trunc .

 

1. C++?

2. C-?

3. - ?

4. ?

5. ?

6. -?

7. ?

8. ?

9. -?

10. -?

11. ?

12. ?

13. ?

14. clear()?

1. 5.

2. .

3. .

1

, .

 

2

, , .

 

3

, , .

 

4

, , .

 

5

, , .

 

6

, , .

 

7

, , , .

 

8

, , , .

 

9

, , .

 

10

, , .

 

11

, , .

 

12

, , , .

 

13

, , , , .

 

14

, , 0 9 , ,..., , .

 

15

, , , .

 

16

, , .

 

17

, , , .

 

18

, .

 

19

, , , .

 

20

, , .

 

21

, , . , 0 9 , ,..., , , .

 

22

, , (, ..)

 

23

, , , (, ...).

 

24

, .

 

25

, , ( , ).

 

1. .

2. .

3. .

4. .

5. .

6. .

7. .

 





:


: 2017-01-21; !; : 868 |


:

:

.
==> ...

1367 - | 1203 -


© 2015-2024 lektsii.org - -

: 0.085 .