// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
 
#include "stdafx.h"
 
#undef UNICODE
#include "windows.h"
 
#include <iostream>
#include <string>
using namespace std;
 
int _tmain(int argc, _TCHAR* argv[])
{
    HANDLE h = CreateFile 
               ("C:\\temp\\abc.txt", // lpFileName,
                GENERIC_READ | GENERIC_WRITE, // dwDesiredAccess,
                0, // dwShareMode,
                NULL, // lpSecurityAttributes,
                CREATE_ALWAYS, //  dwCreationDisposition,
                FILE_ATTRIBUTE_NORMAL, //   dwFlagsAndAttributes,
                NULL); // _In_opt_ HANDLE                hTemplateFile
 
    if (h == INVALID_HANDLE_VALUE)
    {
        int code = GetLastError ();
        cout << "Error code: " << code << endl;
 
        char * answer = "nic";
 
        FormatMessage
        (
          FORMAT_MESSAGE_ALLOCATE_BUFFER | 
          FORMAT_MESSAGE_FROM_SYSTEM /* |
          FORMAT_MESSAGE_IGNORE_INSERTS */, // DWORD dwFlags
 
              NULL, // LPCVOID lpSource,
              code , // DWORD dwMessageId,
              0, // DWORD dwLanguageId,
              (LPTSTR) & answer, //  LPTSTR lpBuffer,
              0, // DWORD nSize,
              NULL // va_list* Arguments
            );
 
        cout << "Error: " << answer << endl;
 
        // LocalFree (answer);
    }
    else
    {
         cout << "Otevreno" << endl;
 
         char * text = "Nejaky text";
         int len = strlen (text);
         DWORD result;
         // cout << "size " << sizeof (text) << endl;
         // cout << "size " << sizeof (result) << endl;
         BOOL ok;
         ok = WriteFile (h, text, len, &result, NULL /* overlapped */);
         if (ok && result == len) cout << "Zapsano" << endl;
 
         SetFilePointer (h, 0, NULL, FILE_BEGIN);
 
         char buf [1024];
         ok = ReadFile (h, buf, sizeof (buf)-1, &result, NULL /* overlapped */);
         if (ok)
             buf [result] = 0;
         if (ok ) cout << "Precteno " << result << "..." << buf <<  endl;
 
 
 
 
         CloseHandle (h);
    }
 
    cout << "Stop" << endl;
 
    char s [2];
    cin.getline (s, sizeof (s));
 
    return 0;
}
 
file_handles_2017.txt · Last modified: 2017/03/03 11:12 by 147.32.8.115
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki