title: Reading binary data through stdin with cat command on linux
body: <p>I am trying to read binary data through stdin (0) to my program using <code>cat</code> command. My program's task is to change binary to integer or double and write it to the desired file descriptor. <br><br>
  When I run the command: <code>cat data_int.bin | ./myprogram -d</code>, I can not read anything and also the size of input is 0. But when i try: <code>./myprogram -d -I 0     0&lt;data_int.bin</code>, my program can read bytes and succesfully finish.<br>
  <br>
  <br>
  My code:<br></p>
  
  <pre><code>#libraries
  
  int main(int argc, char* argv[]) {
  
          int c;
          extern char *optarg;
          extern int optind;
          extern int optopt;
  
          char input_file[100] = { 0 };
          int nastavljen_input = 0;
          char output_file[100] = { 0 };
          int nastavljen_output = 0;
          int tip = -1; // 0 - char, 1- int, 2 - double
          int fd_in = 0;
          int fd_out = 1;
          while((c = getopt(argc,argv, ":o:i:O:I:cdf")) != -1) {
              switch(c) {
                  case 'o':
                      strcpy(output_file,optarg);
                      nastavljen_output = 1;
                      fd_out = open(output_file,O_WRONLY);
                      break;
                  case 'i':
                      strcpy(input_file,optarg);
                      nastavljen_input = 1;
                      fd_in = open(input_file,O_RDONLY);
                      break;
                  case 'O':
                      fd_out = atoi(optarg);
                      break;
                  case 'I':
                      fd_in = atoi(optarg);
                      break;
                  case 'c':
                      tip = 0;
                      break;
                  case 'd':
                      tip = 1;
                      break;
                  case 'f':
                      tip = 2;
                      break;
              }
          }
  
          if(tip &gt; -1) {
              struct stat st;
              fstat(fd_in, &amp;st); //fd_in would be 0 with cat command
              int size = st.st_size; // number of bytes in input file
              printf("%d\n",size); // this will print out 0 with cat command
  
              unsigned char buffer[size];
              read(fd_in,buffer,size);
  
              ...code continues...
  </code></pre>
  
  <p><br><br>
  Flag -d is for reading bytes representing integers and -I is for choosing input file descriptor. Output is stdout(1) in this case.
  <br>
  <br>
  My question is, it there a problem with my code or is this just the way <code>cat</code> command works? I am using Xubuntu.
  <br><br>
  Thank you for your time and effort,<br>
  Domen</p>