bothge
bothge

Reputation: 17

char *fgets(char *str, int n, FILE *stream)

I know how this function works. I know that stdin is often used as a *stream. It's rather question of interest, but is there any function else to be used for input instead of stdin?

Upvotes: 0

Views: 142

Answers (1)

First of all, you use stdin not as *stream but stream, as stdin is declared as

FILE *stdin;

Secondly, stdin is not a function, but an object - a pointer to an opaque structure describing an open stream; or as the name says, an open file. stdin is special in that in programs it is by default be connected to interactive input, i.e. whatever you type with your keyboard.

While stdin is an input stream readily provided to your program before main is executed, you can open many more streams with for example the fopen function:

FILE *a_file = fopen("myinput.txt", "r");

will open the file myinput.txt in the current working directory of the process for reading, and return a pointer to the structure describing the stream of bytes; calling fgets(..., a_file); will now read in consecutive lines of the said file starting from the beginning.

After reading the stream must be closed with fclose(a_stream);

Upvotes: 1

Related Questions