0

My method suppose to switch text output between text file and stdin. So I been thinking about something like:

    void _connection(char mode) {   /* pass 'v' as an argument to set verbous mode*/
      FILE *stream
      
      if (mode == 'v')
        fopen(stream, "stdin location?");
      else
        fopen(stream, "../stream");

      fprintf(stream, "Connecting to the queue...");
      ...

Searching my system I been not able to find any stdin declaration. The stdio.h source file, on my system, is using this macro, but I can't get where it get it from. I mean, there is no inputs file or any macro called stdin in this file.

siery
  • 467
  • 3
  • 20

2 Answers2

2

Find it here (Unix):

/usr/include/stdio.h

Or on Windows:

(Compiler Path)\include\stdio.h

Note that the ISO C standard requires stdin/stdout/stderr to be macros, while POSIX requires them to be external FILE* identifiers, so you may see this on POSIX-compliant systems (Linux and others):

#define stdin stdin
iBug
  • 35,554
  • 7
  • 89
  • 134
  • 1
    The C standard (any version) requires them to be macros, "which are expressions of type ‘‘pointer to FILE’’ that point to the FILE objects associated, respectively, with the standard error, input, and output streams." – Lundin Nov 22 '17 at 14:58
  • POSIX avoids contradicting the C standard and requires macros, too: _The [``](http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/stdio.h.html) header shall define the following macros which shall expand to expressions of type "pointer to FILE" that point to the FILE objects associated, respectively, with the standard error, input, and output streams: `stderr` Standard error output stream. `stdin` Standard input stream. `stdout` Standard output stream._. Nothing stops the macros being `#define stdin stdin` etc. On a Mac, they're mapped to internal names: `#define stdin __stdinp`. – Jonathan Leffler Nov 22 '17 at 15:44
2

The stdin macro is defined either in stdio.h or a file that it eventually includes. As long as you #include <stdio.h> you'll have access to it.

For what you're doing, you don't need to perform a separate open. Just copy stdin to your file pointer:

  if (mode == 'v')
    stream = stdin;
  else
    fopen(stream, "../stream");
dbush
  • 205,898
  • 23
  • 218
  • 273
  • No, the stdin macro is defined in stdio.h, period. This is explicitly specified by 7.21.1. – Lundin Nov 22 '17 at 14:57
  • 2
    @Lundin If the `` header consists entirely of `#include `, and that defines `stdin`, then I would argue that this qualifies as `` defining `stdin`. – Daniel H Nov 22 '17 at 15:21
  • However, when it comes time to `fclose(stream)`, you might want to avoid doing that if you previously set `stream = stdin;`. – rici Nov 22 '17 at 15:25
  • @DanielH The standard explicitly says that stdin is a macro and _the macro_ is defined in stdio.h. This isn't something to debate, C11 7.21.1. As for the contents of the macro, that may very well be something like an external pointer variable declaration. – Lundin Nov 22 '17 at 15:36
  • 1
    I’m not arguing about it being a macro. I’m saying that the phrase “The header *``* defines several macros” does not mean that there is a file called `stdio.h` somewhere on the filesystem which has a `#define` in it. The contents of `` might be in a file not called `stdio.h`, or they might not be in any file, but hardcoded into the compiler or generated on the fly. See 6.10.2p2. If the implementation defines “the entire contents of the header” to be “the pre-processed contents of the file /usr/include/stdio.h”, then that file can include its own files which define the macros. – Daniel H Nov 22 '17 at 16:13
  • Yes, thats what I did when I thought a while longer about it. Thank you for your effort. – siery Nov 22 '17 at 19:48