/************************************************/
/* soubor IO-PV04.C				*/
/* vytvori  textovy soubor SOUBOR.TXT		*/
/* verze s primym pristupem - open		*/
/************************************************/

#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#include <process.h>
#include <string.h>
#include <sys\stat.h>

int chyba(int er)
{
 extern int errno;
 if (er != 0)
   {
    perror(strerror(errno));	/* zapise chybove hlaseni do stderr */
		exit(errno);		/* -> OS; ukonci chod programu	    */
   }
 return er;
} /* int chyba(void) */

int main(void)
{
 int handle, i;
 char s[81], *jmeno = "soubor.txt";

 if ((handle = open(jmeno, O_TRUNC | O_CREAT | O_TEXT | O_WRONLY, S_IWRITE)) == -1)
   chyba(handle);

 strcpy(s, "Toto je textovy soubor vytvoreny v C.\n"
					 "primy pristup - handle\n");
 write(handle, s, strlen(s));
 for (i = 1; i < 10; i++)
   {
    sprintf(s, "%5d", i);
    write(handle, s, strlen(s));
   } /* for (i = 1; i < 10; i++) */

 strcpy(s, "\n\n");
 write(handle, s, strlen(s));

 strcpy(s, "Uz to umim!\n");
 write(handle, s, strlen(s));

 strcpy(s, "Jeste pridat posledni radek na konec.");
 write(handle, s, strlen(s));


 if (close(handle) == -1)
   chyba(1);

 return 0;
} /* int main(void) */