// pokus2.cpp : Defines the entry point for the console application.
//
 
#include "stdafx.h"
#include <windows.h> // CreateFile, ...
#include <stdio.h> // 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;
}
 
filehandles2.txt · Last modified: 2012/02/20 14:57 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