/********************************/
/* soubor IO-PV05.C */
/* precte a vytiskne obsah */
/* textoveho souboru SOUBOR.TXT */
/* pouziva UNIXove funkce */
/********************************/
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#include <process.h>
#include <sys\stat.h>
#define MAX_DELKA_RADKU 81 /* plus jedna */
int cti_radek(int hdl, char *radek, int max)
/************************************************/
/* Do *radek nacte ze souboru hdl radek textu */
/* dlouhy nejvice max znaku. */
/* vrati: -1 pri neuspesnem cteni */
/* 0 pri konci souboru */
/* +n pri uspechu (pocet znaku) */
/* vyjimka - prazdny radek - n == 1 */
/************************************************/
{
int i = 0, kod;
char *s = radek, ch = 0;
while ((i < max) /* neprekrocena max. delka */
&& ((kod = read(hdl, &ch, sizeof(char))) == sizeof(char))
/* ^ korektne nacteno a dosud neni konec */
&& (ch != '\n')) /* neni konec radku */
{
s[i] = ch;
i++;
}
s[i] = '\x0';
return (kod > 0) ? ((i == 0) ? kod : i) : kod;
} /* int cti_radek(int hdl, char *radek, int max) */
int chyba(int er)
{
extern int errno;
if (er != 0)
{
perror(strerror(errno));
exit(errno);
}
return(0);
} /* int chyba(void) */
int main(void)
{
int handle, nacteno;
char s[MAX_DELKA_RADKU], *jmeno = "soubor.txt";
if ((handle = open(jmeno, O_TEXT, S_IREAD)) == -1)
chyba(handle);
printf("\nCteni textoveho souboru <%s> funkcemi s primym pristupem\n",
jmeno);
while ((nacteno = cti_radek(handle, s, sizeof(s) - 1)) > 0)
{
printf("%s\n", s);
}
if (nacteno == 0)
printf("%s\n\n...KONEC...\n", s);
else
chyba(nacteno);
if (close(handle) == -1)
chyba(1);
return(0);
} /* int main(void) */