/********************************/
/* soubor IO-DP02.C */
/* precte a vytiskne obsah */
/* textoveho souboru SOUBOR.TXT */
/* verze pouziva stream */
/* pouziva UNIXove funkce */
/********************************/
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#include <process.h>
#include <sys\stat.h>
#include <string.h>
#define MAX_DELKA_RADKU 81 /* plus jedna */
int cti_radek(FILE *f, char *radek, int max)
/************************************************/
/* Do *radek nacte ze streamu f radek textu */
/* dlouhy nejvice max znaku. */
/* vrati: EOF pri neuspesnem cteni */
/* +n pri uspechu (pocet znaku) */
/* vyjimka - prazdny radek - n == 1 */
/************************************************/
{
int i = 0, ch = 0;
char *s = radek;
while ((i < max) /* neprekrocena max. delka */
&& ((ch = getc(f)) != EOF)
/* ^ korektne nacteno a dosud neni konec */
&& (ch != '\n')) /* neni konec radku */
{
s[i] = ch;
i++;
}
s[i] = '\x0';
return (ch == EOF) ? ch : ((i == 0) ? 1 : i);
} /* 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 er;
} /* int chyba(void) */
int main(void)
{
int nacteno;
FILE *soubor;
char s[MAX_DELKA_RADKU], *jmeno = "soubor.txt";
if ((soubor = fopen(jmeno, "rt")) == NULL)
/* ^ otevirame textovy stream pro cteni */
chyba(1);
printf("\nCteni textoveho souboru <%s> funkcemi pro datovy proud\n",
jmeno);
while ((nacteno = cti_radek(soubor, s, sizeof(s) - 1)) != EOF)
/* while (fgets( s, sizeof(s) - 1, soubor) != NULL) */
{
printf("%s\n", s);
*s = 0x0;
}
if ((nacteno == EOF) && (strlen(s) != 0))
printf("%s\n\n...KONEC...\n", s);
else
chyba(nacteno);
if (fclose(soubor) == -1)
chyba(1);
return 0;
} /* int main(void) */