File descriptors and streams and co

hamilton at osiris.cso.uiuc.edu hamilton at osiris.cso.uiuc.edu
Wed Apr 26 09:23:00 AEST 1989


rds95 at leah.Albany.Edu says:

> I want to be able to make "stdin" read from someplace besides, well,
> standard input in the middle of my program, and then go back to where
> it was again.

i would do it like this:

	void fexchange(a, b)
	FILE *a;
	FILE *b;
	{
		FILE tmp;

		tmp = *a;
		*a = *b;
		*b = tmp;
	}

	FILE *my_file;

	/* open my_file */

	/* exchange stdin & my_file */
	fexchange(stdin, my_file);

	/* ... use stdin ... */

	/* restore stdin */
	fexchange(stdin, my_file);

	fclose(my_file);

i've been using this technique for years in an application where i
wanted a nested "#include" capability for interactive input.

	wayne hamilton
	U of Il and US Army Corps of Engineers CERL
UUCP:	{convex,uunet}!uiucuxc!osiris!hamilton
ARPA:	hamilton at osiris.cso.uiuc.edu	USMail:	Box 476, Urbana, IL 61801
CSNET:	hamilton%osiris at uiuc.csnet	Phone:	(217)333-8703



More information about the Comp.lang.c mailing list