Examination_IT / ComputerScience_LaboratoryWork_ForStudent
.pdf201
( 3 (./ 4- 6(1 ( 4
1.e A ?
2.! > A > ?
3.! ?
4.! -
?
5.D , AB
A A.
6.D , AB
A A.
7.! > ?
8.! ,
?
9.e UTS?
10.! UTS A ?
11.! > B .
12.! > B
.
13.e ?
14.! > C++ -
.
15.! C ?
202
17.
9 % % % (&1 9 ! &1 -' 1 17. 17.
9 % % % (&1 9 ! &1 -' 1
17.1. %-./ +E(34
G A -
>.
17.2. -( -3,D- ,- 6-*- ,
D B -
C -
. P AB C
, B -
>. D , -
, A . = -
.
+E(3+ 3- 3(64>, 9+5.+>,
> C++
– :
ofstream – ( ) >; ifstream – ( ) >; fstream – .
; ofstream
. 17.1, istream – . 17.2.
203
2 17.1
|
. > ( ) ofstream |
|
|
|
|
( |
% |
|
|
||
|
||
open |
. > > |
|
is_open |
-B true, > false – |
|
put |
` > |
|
write |
` > ( ) |
|
seekp |
D B |
|
|
> |
|
close |
` >. |
|
|
2 17.2 |
|
|
. > ( ) ifstream |
|
|
|
|
( |
% |
|
|
||
|
||
open |
. > > |
|
is_open |
-B true, > false – |
|
get |
e |
|
getline |
e |
|
read |
e > ( ) |
|
seekg |
D B |
|
|
> |
|
close |
` > |
>
AB:
1)^ ofstream, ifstream
fstream;
2)>, A > A open, -
C;
3)> B A > write read;
4)> B A > close.
- 17.1 > -
> AB >: B
LIM_ARRAY , ( -
n8), A
204
. = >, >,
.
, 3, 0 17.1. #,.- InTextFile.cpp
#include "stdafx.h" |
|
#include <iostream> |
// ! ofstream. |
#include <fstream> |
|
#include <cstringt.h> |
// ! _MAX_PATH. |
#include <iomanip> |
// : % . |
#define LIM_ARRAY 11 |
|
void _tmain() |
|
{ |
|
using namespace std;
char szCurrentDir[_MAX_PATH]; char szNameFile[_MAX_PATH]; double faNumber[LIM_ARRAY];
int |
i, j; |
// J J |
int |
count; |
//e L % %. ::GetCurrentDirectory(_MAX_PATH, szCurrentDir);
//~ LIM_ARRAY ! % ! :e L I %
//9 L !.
for(i=0; i<LIM_ARRAY; i++)
faNumber[i] = -10.0 + (double)rand()/(RAND_MAX + 1)*20.0;
// R % % QI JL IJ. setlocale(LC_CTYPE, "Russian_Russia.1251");
cout << "" K L I ;> "; cin >> szNameFile;
//z K L ! ;. strcat_s(szNameFile, _MAX_PATH, ".txt");
//I! € file ! I % ! J K L. ofstream file;
//r J JL K L szNameFile. file.open(szNameFile);
//J ! K L % % ! ANSI
file << " e % %: " << szCurrentDir << endl;
//J ! K L I % J J . file << "z : " << LIM_ARRAY << endl;
//J ! K L . J ! J
for(count = 0, i=0; i<LIM_ARRAY/2; i++)
205
{
for(j=0; j<2; j++)
file << scientific << setprecision(12)
<<setfill(' ') << setw(22) << right
<<faNumber[2*i+j];
file << endl; count++;
}
//! 9 J ! K L . J ! % ! ::
//% : .
for(j=0; j<LIM_ARRAY%2; j++)
file << scientific << setprecision(12)
<<setfill(' ') << setw(22) << right
<<faNumber[2*count+j];
file << endl;
// k J K L. file.close();
cout << endl;
cout << "( J I % J K L: \"" << szNameFile; cout << "\"" << endl;
system("pause");
}
H. 17.1. ; > out.txt InTextFile
@ 17.2 >
>, > B ( 17.1)
> R . = >,
, . H-
. 17.2.
206
, 3, 0 17.2. #,.- FromTextFile.cpp
#include "stdafx.h" |
|
|
|
#include <iostream> |
// ! |
ofstream |
|
#include <fstream> |
|||
#include <cstringt.h> |
// |
! |
_MAX_PATH |
#include <iomanip> |
// |
: % |
int _tmain(int argc, _TCHAR* argv[])
{
using namespace std;
char szCurrentDir[_MAX_PATH]; char szNameFile[_MAX_PATH]; char szBuffer[255];
double *pfNumber;
int n; // K L. int i;
// R % % QI JL IJ. setlocale(LC_CTYPE, "Russian_Russia.1251");
cout << "" K L I ;> "; cin >> szNameFile;
//z K L ! ;
strcat_s(szNameFile, _MAX_PATH, ".txt");
//I! € file ! ! J I K L ifstream file;
//r J K L szNameFile. file.open(szNameFile); if(!file.is_open())
{
cout << "r; J K L \"" << szNameFile; cout << "\"" << endl;
cout << " I." << endl; system("pause");
return 1;
}
//| I K L
file.getline(szCurrentDir, _MAX_PATH);
//J ! _ % L
cout << endl << szCurrentDir << endl << endl;
// | K ! : "z :".
file |
>> |
szBuffer >> szBuffer; |
|
file |
>> |
n; |
// . |
//( I J! % Q ! . pfNumber = new double[n];
//R J ! L , Q:
//! 12 K I I "+".
209
, 3, 0 17.3. #,.- InBinaryFile.cpp
#include "stdafx.h" |
|
|
|
#include <iostream> |
// |
! |
ofstream. |
#include <fstream> |
|||
#include <windows.h> |
// |
! |
K GetDiskFreeSpace. |
void _tmain()
{
setlocale(LC_CTYPE, "Russian_Russia.1251"); using namespace std;
const int nNumb = 10; |
// |
|
int nDigital[nNumb]; |
// |
|
//" I % ! L J . int nRangeMin = 10, nRangeMax = 100;
//" K L.
char szNameFile[] = "MyBiFile.bin";
//z L . unsigned long dwBytesPerSector;
//z . unsigned long dwSectorsPerCluster;
//z ! J . unsigned long dwNumberOfFreeClusters;
//r e . unsigned long dwTotalNumberOfClusters; int i;
//K : ! ! C: GetDiskFreeSpace("C:\\", &dwSectorsPerCluster,
&dwBytesPerSector, &dwNumberOfFreeClusters, &dwTotalNumberOfClusters );
//~ ! % ! :e L I %
//9 L !.
for(int i=0; i<nNumb; i++)
nDigital[i] = nRangeMin + rand()%(nRangeMax - nRangeMin);
//I! € fi ! I % ! J K L ofstream fi;
//r J JL K L szNameFile. fi.open(szNameFile , ios::binary);
//k % Q J ! J.
fi.write((char*)&dwBytesPerSector, sizeof(DWORD)); fi.write((char*)&dwSectorsPerCluster, sizeof(DWORD)); fi.write((char*)&dwNumberOfFreeClusters, sizeof(DWORD)); fi.write((char*)&nNumb, sizeof(int));
// k % Q dNumb J !.
210
fi.write((char*)nDigital, nNumb*sizeof(nDigital[0])); fi.close();
// J ! K % 9.
cout << "( J, I % J JL K L " ; cout << szNameFile << ":" << endl << endl;
cout << |
"BytesPerSector |
= "; |
cout << dwBytesPerSector << endl; |
||
cout << |
"SectorsPerCluster |
= "; |
cout << dwSectorsPerCluster << endl; |
||
cout << |
"NumberOfFreeClusters |
= " << |
dwNumberOfFreeClusters << endl; |
||
cout << endl; |
|
|
for(i=0; i<nNumb; i++) |
"; |
|
cout << nDigital[i] << " |
||
cout << endl; |
|
system("pause");
}
D FromBinaryFile ( 17.4) -
> MyBiFile.bin > A [ -
( . 17.4).
H. 17.4. H FromBinaryFile
, 3, 0 17.4. #,.- FromBinaryFile.cpp
#include "stdafx.h"
#include <iostream> |
// |
! |
cout, cin. |
#include <fstream> |
// |
! |
ofstream. |
#include <locale.h> |
|
|
|
#include <windows.h> |
|
|
|