![]() |
| | #1 |
| Registered User Join Date: Aug 2007 Location: MD, USA
Posts: 20
| check stdin for piped data without blocking 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;
}
}
Something like <conio.h>'s getch() I guess... Last edited by HowardL; 10-19-2009 at 09:29 AM. |
| HowardL is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |