0
Public static void main(args){
     String input = Console.ReadLine();
     Console.WriteLine(input);
}

Instead of typing-in the value for 'input' variable using keyboard, Would it be possible to load input values using a file?

I do not want to use 'args' to pass values.

For example: For Online C# editors, we would type-in input values inside a <text-area> and this would be automatically sent to Console.ReadLine() methods.

EDIT: This isn't my production code nor I have any intention to use for work. This is just to understand how online C# editors would run the program with the STDIN data/values provided inside <text-area>

wenn32
  • 1,256
  • 2
  • 17
  • 26
  • "this would be automatically sent to Console.ReadLine() methods" - it would be better to abstract out the "get information" part, to *either* call `Console.ReadLine()` *or* get the data from somewhere else. Why pretend it's coming from the console when it's not? – Jon Skeet May 09 '21 at 06:53
  • 2
    (As it happens you *can* call `Console.SetIn`, but I'd suggest changing the design of your code to read from an arbitrary `TextReader` rather than doing that.) – Jon Skeet May 09 '21 at 06:54
  • Even after the edit, I'm still unclear about what you're trying to do. Are you trying to *create* an online C# editor that can handle `Console.ReadLine`? If so, I suspect that it would be a matter of launching the executable and passing the data into its standard input... – Jon Skeet May 09 '21 at 07:00
  • @JonSkeet agreed. Can you tell me how does online C# compiler accomplish this task? do they have an extension method added on to the final compilation code to make this work without performing any changes on our code? – wenn32 May 09 '21 at 07:00
  • You should look into the .Net File API [MSDN File Class](https://learn.microsoft.com/en-us/dotnet/api/system.io.file?view=net-5.0) – Ethan.S May 09 '21 at 07:02
  • 1
    On the bash shell, you can feed whatever you want to a program using the pipe `|`, so e.g. `cat input.txt | mono YourCSharpProgram.exe` would use the contents of input.txt as the input for your C# program. Not sure whether this is what you are looking for... – Sweeper May 09 '21 at 07:02
  • @wenn32: They're launching a process: they can pass that process whatever input they want. That isn't C# specific at all, nor does it require any changes in the code. – Jon Skeet May 09 '21 at 07:02
  • @JonSkeet "If so, I suspect that it would be a matter of launching the executable and passing the data into its standard input..." yup exactly sir. Do you know if Visual studio or others IDE have feature to do that ? or should I roll out something on my own? Basically I have set of 100 line inputs and I would like to automate it rather than type it in STDIN. – wenn32 May 09 '21 at 07:03
  • Are you looking for [`Process.StandardInput`](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.standardinput?view=net-5.0#System_Diagnostics_Process_StandardInput)? Remember to set [`RedirectStandardInput`](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo.redirectstandardinput?view=net-5.0#System_Diagnostics_ProcessStartInfo_RedirectStandardInput) to true. – Sweeper May 09 '21 at 07:08
  • 1
    Article about console/command-line redirections/pipes : https://ss64.com/nt/syntax-redirection.html –  May 09 '21 at 07:14
  • 1
    Does this answer your question? [How do I use a pipe to redirect the output of one command to the input of another?](https://stackoverflow.com/questions/14574170/how-do-i-use-a-pipe-to-redirect-the-output-of-one-command-to-the-input-of-anothe) & [Piping in a file on the command-line using System.Diagnostics.Process](https://stackoverflow.com/questions/850802/piping-in-a-file-on-the-command-line-using-system-diagnostics-process) & [C# Console receive input with pipe](https://stackoverflow.com/questions/199528/c-sharp-console-receive-input-with-pipe) –  May 09 '21 at 07:15
  • 1
    The Console API should already work as expected, if you pipe a file to stdin, for example `yourexe < thefile` – Marc Gravell May 09 '21 at 07:30
  • "Basically I have set of 100 line inputs and I would like to automate it rather than type it in STDIN." What *exactly* is your context here? Are you trying to build an online C# editor, or are you trying to run an existing program using files as input? Those are two specific but different problems, and we'd be in a much better position to help you if you could be clear about what you're trying to achieve. – Jon Skeet May 09 '21 at 07:32
  • @MarcGravell exactly what I was looking for :) – wenn32 May 09 '21 at 07:36

2 Answers2

3

Thanks @Marc Gravell and @Olivier Rogier.

Answer is to use pipes.

myconsoleapp main.cs

Public static void main(args){
     String input = Console.ReadLine();
     Console.WriteLine(input);
}

input.txt file

Hello world

Command-line

 myconsoleapp.exe < input.txt

output

Hello world

Reference: ss64.com/nt/syntax-redirection.html

How-to: Redirection
   command > filename        Redirect command output to a file

   command >> filename       APPEND into a file

   command < filename        Type a text file and pass the text to command

   commandA  |  commandB     Pipe the output from commandA into commandB

   commandA &  commandB      Run commandA and then run commandB
   commandA && commandB      Run commandA, if it succeeds then run commandB
   commandA || commandB      Run commandA, if it fails then run commandB

   commandA && commandB || commandC
                             If commandA succeeds run commandB, if commandA fails run commandC
                             ( Note that if commandB fails, that will also trigger running commandC )
wenn32
  • 1,256
  • 2
  • 17
  • 26
  • Note that this doesn't answer the question that was asked, particularly "This is just to understand how online C# editors would run the program with the STDIN data/values provided" - please use this as a learning experience in terms of trying to ask questions which are really clear in intent. The goal of "use a text file for an existing program" is really different from "write an online editor that is able to use a text area for input". – Jon Skeet May 09 '21 at 12:28
  • @JonSkeet Sorry for the confusion :) English isn't my first language and I assumed the following line "Instead of typing-in the value for 'input' variable using keyboard, Would it be possible to load input values using a file?" was good enough and I just used "online C# compiler" as an example to help everyone understand. Will try my best to make it understandable for everyone. – wenn32 May 09 '21 at 16:49
  • That sentence you quoted was fine - but bringing the online C# compiler into it made the whole thing much more confusing. It's much simplest if you stick to *one* goal. – Jon Skeet May 09 '21 at 18:29
2

You can use Console.SetIn() method to change the stream from console to a text file. In this way, you will read from the filename.txt directly using Console.ReadLine().

filename.txt>>Hello World

TextReader inp = File.OpenText(path: @"FullPath\filename.txt");
Console.SetIn(inp);
string s = Console.ReadLine();
Console.WriteLine(s);

Output: Hello World

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Yasir
  • 121
  • 7