Mail Archives: djgpp/1998/06/05/16:50:18
Dear Eli Zaretskii,
The appended program demonstrates a problem that is revealed via
setvbuf(). There are two tests. Except for one line of code,
these tests are identical. The line of code that differs in
the tests is the one that is commented out.in the first test.
It reads
//c = getc(Tfile); /* <<<---------- */
In the second test, this line is uncommented.
The purpose of producing these tests is to demonstrate that the
I/O system is failing somewhere. In the first test, a file is
opened, closed, and then reopened. Then, setvbuf() is applied
to the file pointer and the first character is read. It is EOF.
This is a failure because the first line of code begins with '#'.
The first test is needlessly complicated because I wanted to have two
tests that were identical except for one line of code.
What this means is that setvbuf() cannot be used unless extraordinary
measures are taken.
K.B. Williams
==================================================================
#
/* tststvbf.c */
#include <stdio.h>
#include <stdlib.h>
// ==================================================================
// print FILE stream structure
// ==================================================================
void
PrtFileStru(FILE * f)
{
fprintf(stderr,
"Structure for FILE stream associated with file handler %d:\n",
fileno(f));
fprintf(stderr,
"\t_cnt = %d\n"
"\t_ptr = %p\n"
"\t_base = %p\n"
"\t_bufsiz = %d\n"
"\t_flag = %d\n"
"\t_file = %d\n"
"\t_name_to_remove = %s\n",
f->_cnt ,
f->_ptr ,
f->_base ,
f->_bufsiz ,
f->_flag ,
f->_file ,
f->_name_to_remove );
}
// ==================================================================
// main
// ==================================================================
#include <assert.h>
#include <conio.h>
#include <ctype.h>
void Test1(void);
void Test2(void);
int main()
{
Test1();
Test2();
return 0;
}
void Test1()
{
int c = 0, CloseStatus, vbufStatus;
static
char Buffer[1024];
char *FileName = "tststvbf.c", *ReadOnly = "r";
FILE *Tfile;
fprintf(stderr, "= = = = = = = = = = TEST # 1 = = = = = = = = = =\n");
Tfile = fopen(FileName, ReadOnly);
assert(Tfile);
fprintf(stderr, "\nImmediately After Opening File %s:\n", FileName);
PrtFileStru(Tfile);
// If the next line of code is omitted, the
// section labelled TESTING SETVBUF will fail
//c = getc(Tfile); /* <<<---------- */
if (c > 0)
{
fprintf(stderr, "\nFirst Character Read from File: %c\n", c);
fprintf(stderr, "\nImmediately After Reading Character:\n");
PrtFileStru(Tfile);
}
CloseStatus = fclose(Tfile);
assert(CloseStatus == 0);
fflush(stderr);
// TESTING SETVBUF
// ---------------
fprintf(stderr, "\nTESTING SETVBUF\n");
Tfile = fopen(FileName, ReadOnly);
assert(Tfile);
fprintf(stderr, "\nImmediately After Re-Opening File %s:\n", FileName);
PrtFileStru(Tfile);
vbufStatus = setvbuf(Tfile, Buffer, _IOFBF, sizeof(Buffer));
assert(vbufStatus == 0);
fprintf(stderr, "\nImmediately After executing setvbuf():\n");
PrtFileStru(Tfile);
c = getc(Tfile);
fprintf(stderr, "First Character Read from File: %4x = ", c);
if (c == EOF) fprintf(stderr, "EOF\n");
else fprintf(stderr, "%c\n", c);
fprintf(stderr, "\nImmediately After Reading Character:\n");
PrtFileStru(Tfile);
}
void Test2()
{
int c = 0, CloseStatus, vbufStatus;
static
char Buffer[1024];
char *FileName = "tststvbf.c", *ReadOnly = "r";
FILE *Tfile;
fprintf(stderr, "\n= = = = = = = = = = TEST # 2 = = = = = = = = = =\n");
Tfile = fopen(FileName, ReadOnly);
assert(Tfile);
fprintf(stderr, "\nImmediately After Opening File %s:\n", FileName);
PrtFileStru(Tfile);
// If the next line of code is omitted, the
// section labelled TESTING SETVBUF will fail
c = getc(Tfile); /* <<<---------- */
if (c > 0)
{
fprintf(stderr, "\nFirst Character Read from File: %c\n", c);
fprintf(stderr, "\nImmediately After Reading Character:\n");
PrtFileStru(Tfile);
}
CloseStatus = fclose(Tfile);
assert(CloseStatus == 0);
fflush(stderr);
// TESTING SETVBUF
// ---------------
fprintf(stderr, "\nTESTING SETVBUF\n");
Tfile = fopen(FileName, ReadOnly);
assert(Tfile);
fprintf(stderr, "\nImmediately After Re-Opening File %s:\n", FileName);
PrtFileStru(Tfile);
vbufStatus = setvbuf(Tfile, Buffer, _IOFBF, sizeof(Buffer));
assert(vbufStatus == 0);
fprintf(stderr, "\nImmediately After executing setvbuf():\n");
PrtFileStru(Tfile);
c = getc(Tfile);
fprintf(stderr, "First Character Read from File: %4x = ", c);
if (c == EOF) fprintf(stderr, "EOF\n");
else fprintf(stderr, "%c\n", c);
fprintf(stderr, "\nImmediately After Reading Character:\n");
PrtFileStru(Tfile);
}
- Raw text -