// pokus2.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include // CreateFile, ... #include // printf, scanf int _tmain(int argc, _TCHAR* argv[]) { HANDLE h; h = CreateFile ( "c:\\work\\abc.TXT", // pointer to name of the file GENERIC_READ | GENERIC_WRITE, // access (read-write) mode 0, // share mode NULL, // pointer to security attributes OPEN_ALWAYS, // how to create FILE_ATTRIBUTE_NORMAL, // file attributes NULL // handle to file with attributes to copy ); if (h == INVALID_HANDLE_VALUE) { int n = GetLastError (); printf ("CHYBA %d \n", n); } else { printf ("Otevreno \n"); DWORD size, n; void * buf; char * text = "Nejaky text"; buf = text; size = strlen (text); BOOL ok; ok = WriteFile (h, buf, size, &n, NULL); if (ok && n == size) printf ("Zapsano \n"); else printf ("Chyba pri zapisu \n"); size = 100; buf = malloc (size); // new char [size]; SetFilePointer (h, 0, NULL, FILE_BEGIN); ok = ReadFile (h, buf, size, &n, NULL); if (ok) { printf ("Precteno %d bytu \n", n); for (int i = 0 ; i < n ; i ++) { char c = ((char*) buf)[i]; printf ("Byte %d je %c \n", i, c); } } else { printf ("Chyba pri cteni \n"); } CloseHandle (h); } printf ("Stiskni enter \n"); char c; scanf_s ("%c", &c); return 0; }