Thread: check stdin for piped data without blocking

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    MD, USA
    Posts
    71

    check stdin for piped data without blocking

    Hi,
    I want to detect if there is piped data waiting in stdin when my prog starts.
    But it seems that stdin does not even contain an EOF until there is input.
    As a result I get a block... Example:
    Code:
    using System;
    using System.Text; // for ReadKey()
    using System.IO;   // for StreamReader etc
    
    class MyProg
    {
      static int Main(string[] args)
      {
        //string s1;
        //int i;
        char c;
    
        Console.WriteLine("-----test-----");
    
    
    /****  So far everythin has blocked the same as this:         ****/
        while( Console.In.Peek() != -1 ) // blocks at first read... 
        {
          //s1 = Console.ReadLine();
          //Console.Write(s1);
    
          //i = Console.Read();
          //Console.Write("{0} ", i);
    
          c = (char)Console.Read();
          Console.Write("{0} ", c);
    
          //c = Console.ReadKey();   // even with:  using System.Text; still gets:
          //     Error: System.Console' does not contain a definition for `ReadKey'
          //Console.Write("{0} ", c);
        }
    
    /****  This blocks...
        TextReader tIn = Console.In;
        while( tIn.Peek() != -1 )
        {
          c = (char)tIn.Read();
          Console.Write("{0} ", c );
        }
    ****/
    
    /**** This works to read stdin but blocks the same as regular stdin:
    
        StreamReader sin = new StreamReader(Console.OpenStandardInput());
        while( sin.Peek() != -1 )
        {
          c = (char)sin.Read();
          Console.Write("{0} ", c);
          if(c == 'x') break;  
        }
        sin.Close();
    
    #### Sample run:    
    ->  mcs peek_stdio.cs
    ->  mono peek_stdio.exe
    -----test-----
    testxinput
    t e s t x ->  
      
    ****/
    
        return 0;
      }
    }
    I'm still looking for somethig at msdn (on dialaup= drag)
    Something like <conio.h>'s getch() I guess...
    Last edited by HowardL; 10-19-2009 at 09:29 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. HELP!!!!emergency Problem~expert please help
    By unknowppl in forum C++ Programming
    Replies: 9
    Last Post: 08-21-2008, 06:41 PM
  3. Determining if stdin stream holds data
    By Stack Overflow in forum C Programming
    Replies: 3
    Last Post: 01-28-2005, 02:52 PM
  4. can't insert data into my B-Tree class structure
    By daluu in forum C++ Programming
    Replies: 0
    Last Post: 12-05-2002, 06:03 PM
  5. C Programming Question
    By TK in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 07-04-2002, 07:11 PM