#include "stdafx.h" #undef UNICODE #include #include using namespace std; int main() { HANDLE h = CreateFile ("abc.txt", // lpFileName, GENERIC_READ | GENERIC_WRITE, // dwDesiredAccess, 0, // dwShareMode, NULL, // lpSecurityAttributes, CREATE_ALWAYS, // dwCreationDisposition, FILE_ATTRIBUTE_NORMAL, // dwFlagsAndAttributes, NULL // hTemplateFile ); if (h == INVALID_HANDLE_VALUE) { DWORD code = GetLastError (); cout << "Chyba pri otvirani " << code << endl; char * answer = NULL; FormatMessage ( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, // DWORD dwFlags NULL, // LPCVOID lpSource, code, // DWORD dwMessageId, 0, // DWORD dwLanguageId, (LPTSTR)& answer, // LPTSTR lpBuffer, 0, // DWORD nSize, NULL // va_list* Arguments ); if (answer) { cout << "Error: " << answer << endl; LocalFree(answer); } } else { cout << "Otevreno" << endl; char data [80] = "Nejaky text"; int n = strlen (data); DWORD answer; BOOL ok = WriteFile (h, data, n, &answer, NULL); if (ok && answer == n) cout << "Zapsano" << endl; else cout << "Chyba pri zapisovani" << endl; SetFilePointer(h, 0, NULL, FILE_BEGIN); char line[80]; n = sizeof (line) - 1; ok = ReadFile (h, line, n, &answer, NULL); if (ok) { line[answer] = 0; cout << "Precteno " << answer << " bytu: " << line << endl; } if (CloseHandle(h)) cout << "Uzavreno" << endl; } char c; cout << "Napis cokoli a stiskni enter"; cin >> c; return 0; }