#undef UNICODE
#include <windows.h>
 
#include <iostream>
#include <fstream>
using namespace std;
 
int main()
{
    HANDLE h = CreateFile 
      (
       "c:\\temp\\abc.txt", // file name
       GENERIC_READ | GENERIC_WRITE, // access
       0, // share
       NULL, // security attributes
       CREATE_ALWAYS,
       FILE_ATTRIBUTE_NORMAL,
       NULL // template
      );
 
    if (h == INVALID_HANDLE_VALUE)
    {
        // cout << "Nepovedlo se " << GetLastError () << endl;
        int code = GetLastError ();
        cout << "Error code: " << code << endl;
 
        char* answer = (char *) "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;
 
        const char* data = "nejaky text ";
        DWORD size = strlen (data);
        DWORD result;
 
        bool ok = WriteFile (h, data, size, &result, NULL);
        if (ok && result == size)
            cout << "Podarilo se zapsat" << endl;
 
        ok = WriteFile (h, data, size, &result, NULL);
        if (ok && result == size)
            cout << "Podarilo se zapsat" << endl;
 
        SetFilePointer (h, 0, NULL, FILE_BEGIN);
        char text [200];
        size = sizeof (text) - 1;
        ok = ReadFile (h, text, size, &result, NULL);
        if (ok)
        {
            cout << "Precteno " << result << " bytu" << endl;
            text [result] = 0;
            cout << "Text: " << text << endl;
        }
 
        cout << "int ma " << sizeof (int) << " bytu" << endl;
        cout << "long ma " << sizeof (long) << " bytu" << endl;
        cout << "DWORD ma " << sizeof (DWORD) << " bytu" << endl;
        cout << "long long ma " << sizeof (long long) << " bytu" << endl;
        cout << "void * ma " << sizeof (void *) << " bytu" << endl;
 
        CloseHandle (h);
    }
 
    cout << "O.K." << endl;
}
 
zos/create_file_2021.txt · Last modified: 2021/03/29 17:15 by 88.103.111.44
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki