file descriptor

Chris Torek chris at mimsy.umd.edu
Sat Oct 6 14:42:37 AEST 1990


In article <121948 at linus.mitre.org> cazier at mbunix.mitre.org (Cazier) writes:
>How would you legally set a file descriptor to some predetermined value
>as can be done in FORTRAN. For example, I can OPEN(8) or set a value
>IN=8 then OPEN(IN) -- and subsequently WRITE(8) as desired. What's the
>equivalent in C?

I cannot figure out what this question is supposed to mean.  The only
reason one might want to write

	IN=8

and then eventually

	OPEN(IN, ...)

in FORTRAN is because the language's I/O statements require numbers as
arguments and, as a result, people tend to hardcode specific numbers
(such as 5 and 6 for input and output).  The C language's I/O functions
supplied as part of any hosted implementation do not take numeric
values, but rather values of type `FILE *'.  There are only three
predefined `FILE *' values, namely stdin, stdout, and stderr.  Thus,
there are two possible answers:

 1. There is no equivalent because the equivalent of write(8) does
    not occur.  The only `constants' that can be hardcoded into I/O
    calls are stdin, stdout, and stderr.  A program that opens an
    input file does this with code of the form

	FILE *f = fopen(...);
	if (f == NULL) handle_error_no_such_file();
	... do I/O using f, or return f to caller, or whatever ...

 2. The equivalent is freopen, which takes a currently-open file,
    closes it, opens a new one, and puts all the relevant stuff under
    the same `FILE *' value so that I/O operations on that `FILE *'
    refer to the new file.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 405 2750)
Domain:	chris at cs.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.std.c mailing list