2 years ago

#39099

test-img

RdJNL

Redirecting C# console app's stdout also redirects stdin

I'm trying to create a simple C# console app that mimics Linux' cat:

string? inp;
while ((inp = Console.ReadLine()) != null)
{
    Console.WriteLine(inp);
}

I'm running this application in Linux (tested with Alpine and Ubuntu), and it seems to work correctly. Everything I type, gets echoed by the app, similar to cat:

/app # ./ConsoleTest
abc
abc
def
def
123
123

However, when I try to redirect the output to a file, the input doesn't show anymore when I type, but instead gets redirected to the file too:

/app # ./ConsoleTest >test.txt
/app # cat test.txt
abc
abc
def
def
123
123

This behavior is not equal to cat:

/app # cat >test.txt
abc
def
123
/app # cat test.txt
abc
def
123

So the question is: why does this not work and how do I get my application to mimic cat when the output is redirected?

c#

linux

.net-core

cat

0 Answers

Your Answer

Accepted video resources