Thread: Reading STDIN and outputting it from a function

  1. #1
    Registered User
    Join Date
    Apr 2020
    Posts
    3

    Reading STDIN and outputting it from a function

    Hi all, I'm working on an app that will read stdin and simply pass it to a function that ouputs it. I have this so far:
    Code:
    #include <stdio.h>
    
    void code_here(char inputData[]);
    
    
    int main(){
      char arr[512];
      fread(arr, 1, 512, stdin);
      code_here(arr);
      return 0;
    }
    
    
    void code_here(char inputData[]) {
      // Use this function to output your solution
      puts(inputData);
    }
    Things appear to work but I wanted to sanity check here that things look ok. Thanks for any guidance.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    3,824
    FYI: puts requires a zero terminated array; your code does *not* ensure that this is true.

    Edit: Read about the return value of fread.

    Tim S.
    Last edited by stahta01; 04-06-2020 at 07:11 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Apr 2020
    Posts
    3
    Quote Originally Posted by stahta01 View Post
    FYI: puts requires a zero terminated array; your code does *not* ensure that this is true.

    Edit: Read about the return value of fread.

    Tim S.
    Thanks Tim. I read about the size value that fread returns. In my case I'm hitting an EOF I believe. When I examine the output, there is an odd character at the end.
    My first guess would be to strcat() a `\0` to the end of the buffer. If that doesn't work, I'm probably taking the wrong approach.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,748
    Quote Originally Posted by dmarr
    My first guess would be to strcat() a `\0` to the end of the buffer.
    You cannot use strcat because strcat itself expects a null terminated string to concatenate to.

    Rather, as stahta01 implied, fread returns the number of items read, and since you're specifying each item is one byte, it returns the number of bytes read. Use that to directly write a null character (but then you should pass 511, or more flexibly sizeof(arr) - 1, as the third argument to fread since you need to reserve at least one element for the null character).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2020
    Posts
    3
    @laserlight Thank you for your help. I am having a hard time understanding how to "use that to directly write a null character". I used the 511 value in the fread call, but I still see the bad character come back in the result.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,996
    It would be
    Code:
      char arr[512];
      n = fread(arr, 1, sizeof(arr)-1, stdin);
      if ( n > 0 ) {
        arr[n] = '\0';
        code_here(arr);
      }
    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.

  7. #7
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    127
    Code:
    #include "stdio.h"
    #include "string.h"
    
    char* input(char* buffer, size_t size)
    {
     fgets(buffer, size, stdin);
     char* newline = strstr(buffer, "\n");
     if(newline)
      *newline = 0;
     return buffer;
    }
    
    int empty(char* text)
    {
     return text == NULL || *text == 0;
    }
    
    int main(void)
    {
     char data[1024];
     for(;;)
     {
      printf("Input (empty line to exit): ");
      if(empty(input(data, sizeof(data))))
       break;
      puts(data); 
     }
    }
    Code:
    #include <stdio.h> 
    #include <stdlib.h>
    #include <time.h>
    #include <math.h>
    long x(long y,long z){if(!z)return y;return x(z,y%z);}int 
    main(void){long t,u,w=0,v=0;srand(time(0));for(;;){t=rand();
    u=rand();if(x(t,u)-1);else++v;system("clear")&&system("cls");
    printf("\n\n\n %g\n\n\n",1/sqrt(((double)v)/((3<<1)*++w)));}}

  8. #8
    Registered User
    Join Date
    Apr 2020
    Location
    Greater Philadelphia
    Posts
    25
    Quote Originally Posted by Salem View Post
    It would be
    Code:
      char arr[512];
      n = fread(arr, 1, sizeof(arr)-1, stdin);
      if ( n > 0 ) {
        arr[n] = '\0';
        code_here(arr);
      }
    Shouldn't we write '\0' even if n==0, giving us an empty string corresponding to 0 bytes read?

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,996
    > Shouldn't we write '\0' even if n==0, giving us an empty string corresponding to 0 bytes read?
    Only if code_here(arr) actually did anything useful with a zero length string, other than saying "oh, it's zero length, nothing to do".

    The n == 0 case indicates either an error (use ferror() to find out) or end of file (use feof() to find out).
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading and outputting BST
    By MagicWand in forum C++ Programming
    Replies: 1
    Last Post: 05-29-2016, 06:27 AM
  2. I/O files: reading, manipulating and outputting data
    By Mwepak in forum C++ Programming
    Replies: 17
    Last Post: 12-12-2012, 07:50 AM
  3. Reading from stdin
    By windingprince in forum C Programming
    Replies: 2
    Last Post: 08-27-2009, 05:07 AM
  4. reading from stdin
    By carlorfeo in forum C++ Programming
    Replies: 18
    Last Post: 02-06-2008, 08:50 AM
  5. reading from stdin
    By AngKar in forum C Programming
    Replies: 4
    Last Post: 05-03-2006, 12:14 PM

Tags for this Thread