Piping Input into a C++ Program to Debug in Visual Studio

Piping input into a c++ program to debug in Visual Studio

Put your data in a file then go to the project properties in Visual Studio and select the "Debugging" category.

In the "Command Arguments" property type:

< "path/to/the/file"

Now that file will be fed to the program via standard input when the debugger is launched or when you launch the program within Visual Studio (but without the debugger) using Ctrl-F5.

You can use VS macros to specify the project directory, etc. if you want the test file to move along with the project.

How to read input when debugging in C++ in Visual Studio Code?

As stated in Here

if you enable "externalConsole":true in the launch.json then you will get a pop up console window that you can type in.

Visual Studio Code use input text file on debug

I use GDB instead of lldb but still encountered the same issue. It waited for an input when I typed arguments in the "launch.json" file this way:

"args": ["<", "test1.txt"],

But it started working properly when I had rewritten it in the following manner:

"args": ["<", "${workspaceFolder}/test1.txt"],

I think that one should add a parent folder even though the input file is in the workspace folder or simply use the full path.

Use input (stdin) in debug console VScode

In launch.json, you can specify a file to connect to stdin via:

"args": ["<", "/path/to/your/stdin/file"]

Redirect input to .NET Core Console App on debugging in Visual Studio 2019

It's done in the same way. A Properties/launchSettings.json file should be generated, similar to this one:

{
"profiles": {
"YourProjectName": {
"commandName": "Project",
"commandLineArgs": "< input-file.txt"
}
}
}

Please note that, if you're debugging from Visual Studio, you need to make sure your input file is copied to the output folder so that the redirection works.

i.e. you can manually add this to the .csproj:

  <ItemGroup>
<None Update="input-file.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

or, as @paulsm4 suggested, configure it via VS interface: input-file.txt -> Properties -> 'Copy'

How to debug with VSCode and pipe command

If you change "console" to "externalTerminal", when you run the program an external terminal window will open. This window has stdin connected to the keyboard, so if you type or paste content it will be passed to the program until you send or type ctrl-z. stdout goes to the window. Debugging and breakpoints work as expected.

C# Console receive input with pipe

You need to use Console.Read() and Console.ReadLine() as if you were reading user input. Pipes replace user input transparently. You can't use both easily (although I'm sure it's quite possible...).

Edit:

A simple cat style program:

class Program
{
static void Main(string[] args)
{
string s;
while ((s = Console.ReadLine()) != null)
{
Console.WriteLine(s);
}

}
}

And when run, as expected, the output:


C:\...\ConsoleApplication1\bin\Debug>echo "Foo bar baz" | ConsoleApplication1.exe
"Foo bar baz"

C:\...\ConsoleApplication1\bin\Debug>


Related Topics



Leave a reply



Submit