.


:




:

































 

 

 

 





BOOL CopyFile(

LPCTSTR lpExistingFileName, //

LPCTSTR lpNewFileName, //

BOOL bFailIfExists //

);

BOOL MoveFile(

LPCTSTR lpExistingFileName, //

LPCTSTR lpNewFileName, //

);

1. , .

#include <windows.h>

#include <iostream.h>

 

int main()

{

HANDLE hFile;

//

hFile = CreateFile(

"C:\\demo_file.dat", //

GENERIC_WRITE, //

0, //

NULL, //

CREATE_NEW, //

FILE_ATTRIBUTE_NORMAL, //

NULL //

);

//

if (hFile == INVALID_HANDLE_VALUE)

{

cerr << "Create file failed." << endl

<< "The last error code: " << GetLastError() << endl;

cout << "Press any key to finish.";

cin.get();

return 0;

}

//

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

{

DWORD dwBytesWrite;

 

if (!WriteFile(

hFile, //

&i, // ,

sizeof(i), //

&dwBytesWrite, //

(LPOVERLAPPED)NULL //

))

{

cerr << "Write file failed." << endl

<< "The last error code: " << GetLastError() << endl;

CloseHandle(hFile);

cout << "Press any key to finish.";

cin.get();

return 0;

}

}

//

CloseHandle(hFile);

cout << "The file is created and written." << endl;

return 0;

}

 

2. , : , , , , .

#include <windows.h>

#include <iostream>

#include <conio.h>

using namespace std;

int main()

{

int iMenu;DWORD dwBytesWrite;

char cMenu[200]=":\n1 - \n2 - \n3 - \n4 - \n5 - \n6 - \n7 - \n8 - \n";

CharToOem(cMenu, cMenu);

while(1)

{

system("cls");

cout << cMenu; cin >> iMenu;

switch(iMenu)

{

HANDLE hFile;

char buf[50],buf1[50];

case 1:

CharToOem(" :\n ?\n", buf); cout << buf;

cin >> buf;

hFile = CreateFile(buf,GENERIC_WRITE, 0,NULL,CREATE_NEW, FILE_ATTRIBUTE_NORMAL,NULL);

 

if (hFile == INVALID_HANDLE_VALUE)

{

cerr << "Create file failed." << endl

<< "The last error code: " << GetLastError();

cout << "\nPress any key to finish.";

getch();

break;

}

cout << "The file is created." << endl;

CloseHandle(hFile);

getch();

break;

case 2:

CharToOem(" :\n ?\n", buf); cout << buf;

cin >> buf;

hFile = CreateFile(buf,GENERIC_WRITE,0,NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,NULL);

if (hFile == INVALID_HANDLE_VALUE)

{

cerr << "Open file failed." << endl

<< "The last error code: " << GetLastError();

cout << "\nPress any key to finish.";

getch();

break;

}

cout << "The file is opened." << endl;

CloseHandle(hFile);

getch();

break;

case 3:

CharToOem(" :\n ?\n", buf); cout << buf;

cin >> buf;

if (!DeleteFile(buf))

{

cerr << "Delete file failed." << endl

<< "The last error code: " << GetLastError();

cout << "\nPress any key to finish.";

getch();

break;

}

cout << "The file is deleted." << endl;

getch();

break;

case 4:

CharToOem(" :\n ?\n", buf); cout << buf;

cin >> buf;

hFile = CreateFile(buf,GENERIC_WRITE,0,NULL,OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL,NULL);

if (hFile == INVALID_HANDLE_VALUE)

{

cerr << "Creat file failed." << endl

<< "The last error code: " << GetLastError();

cout << "\nPress any key to finish.";

getch();

break;

}

cout << "The file is opened." << endl;

CharToOem(" !\n", buf); cout << buf;

cin >> buf;

if (!WriteFile(hFile,&buf,sizeof(buf),&dwBytesWrite,
(LPOVERLAPPED)NULL))

{

cerr << "Write file failed." << endl

<< "The last error code: " << GetLastError();

CloseHandle(hFile);

cout << "\nPress any key to continue.";

getch();

break;

}

cout << "The Information is written." << endl;

CloseHandle(hFile);

getch();

break;

case 5:

CharToOem(" :\n ?\n", buf); cout << buf;

cin >> buf;

hFile = CreateFile(buf,GENERIC_READ,0,NULL,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,NULL);

if (hFile == INVALID_HANDLE_VALUE)

{

cerr << "Open file failed." << endl

<< "The last error code: " << GetLastError();

cout << "\nPress any key to finish.";

getch();

break;

}

cout << "The file is opened." << endl;

if (!ReadFile(hFile,&buf,sizeof(buf),&dwBytesWrite, (LPOVERLAPPED)NULL))

{

cerr << "Read file failed." << endl

<< "The last error code: " << GetLastError();

CloseHandle(hFile);

cout << "\nPress any key to continue.";

getch();

break;

}

if(!dwBytesWrite) cout << "The file is empty" << endl;

else cout << "Read information:"<< buf << endl;

CloseHandle(hFile);

getch();

break;

case 6:

CharToOem(" :\n ?\n", buf); cout << buf;cin >> buf;

CharToOem(" ?\n", buf1); cout << buf1;cin >> buf1;

if(!CopyFile(buf,buf1,true))

{

cerr << "Move file failed. The last error code: " << GetLastError();

cout << "\nPress any key to continue.";

getch();

break;;

};

cout << "The file is copied."<< endl;

getch();

break;

case 7:

CharToOem(" :\n ?\n", buf); cout << buf;cin >> buf;

CharToOem(" ?\n", buf1); cout << buf1;cin >> buf1;

if(!MoveFile(buf,buf1))

{

cerr << "Move file failed. The last error code: " << GetLastError();

cout << "\nPress any key to continue.";

getch();

break;;

};

cout << "The file is moved."<< endl;

getch();

break;

case 8: return 0;

}

}

}

 

1. 1. CreateFile WriteFile, .

2. :

− ;

− ;

− ;

− , ;

− ;

− ;

− .

, . , .

1. ? .

2. ?

3. : hIn=CreateFile(argv [1], GENERIC_READ, 0, NULL, OPEN_EXISTING, 0,NULL)?

4. , : hOut=CreateFile(argv[2], GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL).

5. , : while (ReadFile(hIn, Buffer, BUF_SIZE, &nIn, NULL) && nIn > 0).

6. : WriteFile (hOut,Buffer, nIn, &nOut, NULL)?

7. , : if(!opyFile(argv[1],argv[2],FALSE)).

9

: Win32 API

:

1. Win32API, .

2. , .





:


: 2016-11-24; !; : 603 |


:

:

, .
==> ...

1490 - | 1279 -


© 2015-2024 lektsii.org - -

: 0.057 .