PRB: Freopen() Fails in QuickWin Libraries

ID Number: Q76664

1.00

WINDOWS

Summary:

SYMPTOMS

The Microsoft C run-time library function freopen() will fail if it

tries to reassign stdin, stdout, or stderr in a QuickWin window.

The sample code below demonstrates this behavior.

CAUSE

This limitation is due to an innate restriction in the QuickWin

implementation. It is illegal to close stdin, stdout, or stderr

under QuickWin (unlike MS-DOS and OS/2). As part of its processing,

the freopen() example below tries to close stdout (before assigning

that stream to another file). The problem with closing stdin, stdout,

or stderr files is that all three streams are assigned to the same

QuickWin file handle. That is, in DOS and OS/2, stdin, stdout, and

stderr are all different file handles, so closing one doesn't affect

others. Under QuickWin, they are all tied to the same file, so

closing one has drastic effects on the others.

RESOLUTION

Do not reassign stdin, stdout, or stderr while in a QuickWin window.

More Information:

Sample Code

-----------

/* Compile options needed: QuickWin EXE

*/

#include <stdio.h>

#include <process.h>

FILE *stream, *errstream;

main()

{ /* Reassign "stdout" to "data2" */

stream = freopen("data2", "w", stdout);

if (stream == NULL ) /* If reassignment failed */

printf("error on freopen\n");

else

{

fprintf(stream,"This will go to the file `data2'\n");

printf("`stdout' successfully reassigned\n");

fclose(stream);

}

exit(0);

}

Additional reference words: QCWin QC/Win 1.00