Thread: C/C++, reading from STDIN using fread....

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    6

    C/C++, reading from STDIN using fread....

    Hi,
    I'm writing some client/server application for my class assignment.
    The client should read the input from STDIN and send it to the socket.
    My program stack on those lines:

    char buffer [STD_BUFF_SIZE];
    printf("Before fread.\n");
    int len = fread(&buffer,sizeof(buffer),1,stdin);
    printf("After fread.\n");

    (I'm passing some text to it's STDIN from some text file...)

    1. The line "Before fread." isn't printed until I press 'enter' key, why is that?
    2. The line "After fread." isn't printed at all, that mean that the fread function never returns, why? how to solve this?


    The client should get the commands that need to be executed on the other side from ARGV and the standard input from it's STDIN.
    I can't use getline because my lecturer told me that the input may be binary. He told me that fread should be good for this purpose. The input from stdin may come in different sizes. How to properly get it into a buffer, so I can send it to the server?

    Thanks in advance.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well the "before" works for me, if all I have is those lines. What else do you have?

    As for the "after", well you won't see that until the buffer is full.
    When the buffer is full, then it will be up to you to "parse the maybe it's binary data" yourself.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main ( ) {
      char      buff[BUFSIZ];
      ssize_t   n;
      printf("Before\n");
      n = fread(buff, 1, sizeof buff, stdin);
      printf("After - read %d bytes\n", (int)n);
      return 0;
    }
    If you've got less text than the size of the buffer, you won't see anything until the EOF marker appears.

    Also note the order of 1,sizeof
    Your way, you only succeed if the buffer is FULL.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    6
    Well, it does eventually works.
    I don't know what size of input to expect. You are telling me that until the buffer filled up I will not see anything? I did test it with a small input (smaller then the buffer) and it works...

    I have another question:
    what's the point of closing stdin descriptor? doesn't it meant to be always open?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    stdin gets closed if input has been redirected to a file say.

    > I don't know what size of input to expect.
    Which is why you typically read a file using a fixed buffer and a while loop.
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main ( ) {
      char      buff[50];
      ssize_t   n;
      int       total = 0;
    
      printf("Before\n");
    
      while ( ( n = fread(buff, 1, sizeof buff - 1, stdin) ) > 0 ) {
        buff[n] = '\0';
        printf("+++%s+++\n",buff);
        total += n;
      }
      
      printf("After - read %d bytes\n", total);
    
      return 0;
    }
    Run on itself, it looks like this
    Code:
    $ ./a.out < foo.c
    Before
    +++#include <stdlib.h>
    #include <stdio.h>
    
    int main +++
    +++( ) {
      char      buff[50];
      ssize_t   n;
      int +++
    +++      total = 0;
    
      printf("Before\n");
    
      while +++
    +++( ( n = fread(buff, 1, sizeof buff - 1, stdin) ) +++
    +++> 0 ) {
        buff[n] = '\0';
        printf("+++%s+++\+++
    +++n",buff);
        total += n;
      }
      
      printf("After +++
    +++- read %d bytes\n", total);
    
      return 0;
    }
    +++
    After - read 337 bytes
    Inside the loop, you either process what you've read so far, or allocate some space to store it for later.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    6
    Ok thanks, another question:

    As i mentioned, i'm building a remote shell.

    My server needs to get the commands it needs to run and then:

    1. use dup2 to connect between what it gets from the socket to it's stdin.
    2. use dup2 to connect between what it inputs to stdout and the socket.

    do I need to close the socket in the server's side after i do (1) ?
    do I need to close stdout in the server's side after i do (2) ?

    Or, those are only optional steps?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fread() questions.
    By gp364481 in forum C Programming
    Replies: 2
    Last Post: 02-06-2009, 12:32 AM
  2. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  3. fread is reading too much
    By CeeCee in forum C Programming
    Replies: 7
    Last Post: 02-18-2002, 03:44 PM
  4. Array, reading in response etc...
    By mattz in forum C Programming
    Replies: 4
    Last Post: 12-05-2001, 11:41 AM
  5. fread() and fwrite() ?
    By Limblet in forum C Programming
    Replies: 4
    Last Post: 09-25-2001, 07:36 PM

Tags for this Thread