Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h>
- #include <iostream>
- using namespace std;
- int main()
- {
- HANDLE hFile;
- BOOL bFile;
- char chBuffer[] = "abc";
- DWORD dwNumBytesToWrite = strlen(chBuffer) + 1;
- DWORD wNumBytesWritten = 0;
- char* chBufferRead = new char[strlen(chBuffer) + 1];
- DWORD dwNumBytesToRead = strlen(chBuffer) + 1;
- DWORD wNumBytesRead = 0;
- hFile = CreateFile(
- L"WriteFile.txt",
- GENERIC_READ | GENERIC_WRITE,
- FILE_SHARE_READ | FILE_SHARE_WRITE,
- NULL,
- CREATE_ALWAYS,
- FILE_ATTRIBUTE_NORMAL,
- NULL);
- if (hFile == INVALID_HANDLE_VALUE)
- {
- cout << "CreateFile failed with Error number: " << GetLastError() << endl;
- }
- else
- {
- cout << "CreateFile Success " << endl;
- }
- //Write to file
- bFile = WriteFile(
- hFile,
- chBuffer,
- dwNumBytesToWrite,
- &wNumBytesWritten,
- NULL);
- if (bFile == FALSE)
- {
- cout << "WriteFile failed with Error" << GetLastError() << endl;
- }
- else
- {
- cout << "WriteFile Success " << endl;
- }
- //Read from file
- bFile = ReadFile(
- hFile,
- chBufferRead,
- dwNumBytesToRead,
- &wNumBytesRead,
- NULL);
- if (bFile == FALSE)
- {
- cout << "ReadFile failed with Error" << GetLastError() << endl;
- }
- else
- {
- cout << "ReadFile Success " << endl;
- cout << "Data read from file: " << chBufferRead << endl;
- }
- delete chBufferRead;
- if (CloseHandle(hFile))
- {
- cout << "Handle closed" << endl;
- }
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement