Mail Archives: djgpp/2000/05/11/07:36:04
Thomas J. Hruska <shinelight AT crosswinds DOT net> wrote:
> In a NORMAL
> programming language (anything other than DJGPP) you can read the last
> character, call feof(FILE *), and it will return that it has indeed reached
> the end of file.
Not in any 'NORMAL' programming language that has to read Unix files,
like C. DJGPP is not a language, it's a compiler implementing one
language, and does behave exactly as required.
To do what you want, the program would have to be
prescient. 'End-of-File' can only reliably be detected by trying to
read something, and finding there's nothing left to read. Otherwise,
reading from stdin or other externally driven 'files' would never
work.
> do
> {
> x[0] = fgetc(fp);
> str = (char *)realloc(str, strlen(str) + 2);
> strcat(str, x);
> } while (!feof(fp) && x[0] != 10);
This loop is contrasting to the usual C idioms, and, unsurprisingly,
caught a bug an doing so. The idiom would be using an int variable to
read to, and compare that to EOF, before assigning the result to a
char-type variable, but to stay closer with your current style, you'ld
have to write
do
{
x[0] = fgetc(fp);
if (feof(gp))
break;
str = (char *)realloc(str, strlen(str) + 2);
strcat(str, x);
} while (!feof(fp) && x[0] != '\n');
Also note the change from a magical number '10' to the character '\n'.
Actually, truely idiomatic progamming would use 'fgets()', instead of
fgetc(), for this, avoiding hundreds of calls to realloc(), by using
an appropriate buffer size.
--
Hans-Bernhard Broeker (broeker AT physik DOT rwth-aachen DOT de)
Even if all the snow were burnt, ashes would remain.
- Raw text -