// OpenFile.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "windows.h"
#include "iostream"
#include "stdio.h"


int _tmain(int argc, _TCHAR* argv[])
{
   // printf ("Opening file\n");

   HANDLE f;
   f = CreateFile (
       "C:\\work\\abc.dat", // 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 (f == INVALID_HANDLE_VALUE)
      std::cout << "Nejde otevrit" << std::endl;
	else
	{
      std::cout << "Soubor otevren" << std::endl;

	  int n = GetFileSize (f, NULL);
	  if (n != 0)
	     SetFilePointer (f, -1, 0, FILE_END);

	  char test;
	  DWORD answer;
	  bool done = ReadFile (f, &test, 1, &answer, NULL);
	  if (done && test != '\n')
	  {
		  test = '\n';
	      WriteFile (f, &test, 1, &answer, NULL);
	  }

	  char data [] = "Nejaky text\r\n";
	  DWORD size = strlen (data);
	  DWORD result;

	  BOOL ok;
	  ok = WriteFile (f, // handle
		              data, // pointer to data
					  size, // number of bytes
					  & result,
		              NULL);

	  if (ok && result == size)
         std::cout << "Data zapsana" << std::endl;
	  else
         std::cout << "Chyba pri zapisu" << std::endl;

	  CloseHandle (f);
	}

	// int wait;
	// std::cout.flush();
	// std::cin >> wait;
	char * str = new char [11];
	std::cin.read(str, 10);

	return 0;
}