C Board  

Go Back   C Board > General Programming Boards > C# Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-19-2009, 09:16 AM   #1
Registered User
 
Join Date: Aug 2007
Location: MD, USA
Posts: 20
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.
HowardL is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 11:41 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22