How to detect if Console.In (stdin) has been redirected?

You can find out by p/invoking the Windows FileType() API function. Here’s a helper class: using System; using System.Runtime.InteropServices; public static class ConsoleEx { public static bool IsOutputRedirected { get { return FileType.Char != GetFileType(GetStdHandle(StdHandle.Stdout)); } } public static bool IsInputRedirected { get { return FileType.Char != GetFileType(GetStdHandle(StdHandle.Stdin)); } } public static bool IsErrorRedirected { … Read more

Intercept calls to console.log in Chrome

You need to call console.log in the context of console for chrome: (function () { var log = console.log; console.log = function () { log.call(this, ‘My Console!!!’); log.apply(this, Array.prototype.slice.call(arguments)); }; }()); Modern language features can significantly simplify this snippet: { const log = console.log.bind(console) console.log = (…args) => { log(‘My Console!!!’) log(…args) } }

Should I use %i or %d to print an integer in C using printf()?

They are completely equivalent when used with printf(). Personally, I prefer %d. It’s used more often (should I say “it’s the idiomatic conversion specifier for int“?). (One difference between %i and %d is that when used with scanf(), then %d always expects a decimal integer, whereas %i recognizes the 0 and 0x prefixes as octal … Read more

How to overwrite stdout in C

It’s not manipulating stdout — it’s overwriting the characters which have already been displayed by the terminal. Try this: #include <stdio.h> #include <unistd.h> static char bar[] = “=======================================” “======================================>”; int main() { int i; for (i = 77; i >= 0; i–) { printf(“[%s]\r”, &bar[i]); fflush(stdout); sleep(1); } printf(“\n”); return 0; } That’s pretty close … Read more

Where does Console.WriteLine go in Debug?

The console can redirect it’s output to any textwriter. If you implement a textwriter that writes to Diagnostics.Debug, you are all set. Here’s a textwriter that writes to the debugger. using System.Diagnostics; using System.IO; using System.Text; namespace TestConsole { public class DebugTextWriter : TextWriter { public override Encoding Encoding { get { return Encoding.UTF8; } … Read more

reading two integers in one line using C#

One option would be to accept a single line of input as a string and then process it. For example: //Read line, and split it by whitespace into an array of strings string[] tokens = Console.ReadLine().Split(); //Parse element 0 int a = int.Parse(tokens[0]); //Parse element 1 int b = int.Parse(tokens[1]); One issue with this approach … Read more

Is it possible to write extension methods for Console? [duplicate]

It’s not possible, as mentioned in Matt’s answer. As a workaround you could create a static class that will wrap Console adding desired functionality. public static class ConsoleEx { public static void WriteLineRed(String message) { var oldColor = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(message); Console.ForegroundColor = oldColor; } } It’s not ideal, as you have to … Read more