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

19,877

Solution 1

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.

Solution 2

You may save your input as a file.(like "intput.txt"). Then call

freopen("intput.txt", "r", stdin);
//code to read from stdin.
fclose(stdin);
Share:
19,877

Related videos on Youtube

Caleb Jares
Author by

Caleb Jares

Updated on June 06, 2022

Comments

  • Caleb Jares
    Caleb Jares almost 2 years

    this has probably been asked before, but I haven't been able to find any answers so far. I'm trying to start my program up with multi-line input, I.E. something I don't want to type in the command line every time (as I'd probably make a mistake). I looked into the command line arguments and I pasted my input in there, but it interpreted it as every line being a command.

    Input in case it helps:

    8
    c j i b s x k j
    t a o a v y z l
    x r t s w o n m
    z y x e n s p r
    l l o b s t e r
    t g x a a a a a
    j p e l a k e k
    t r s l j e e e
    cat
    test
    baseball
    bake
    paste
    lobster
    stake
    zen
    locks
    rake
    gel
    slack
    jar
    dinosaur
    0
    
  • Joe
    Joe about 12 years
    Additionally I usually just add the data file as a regular file in the project, and mark it 'exclude from build'. Then you can just set the arguments in the debugger to '< FileIMade.txt'. Make sure wording directory is set to $(ProjectDir) and you are good to go and the file is available for easy editing access in the project and/or source control.
  • Seng Cheong
    Seng Cheong about 12 years
    If I remember correctly, this isn't documented but works! Its particularly surprising because < is normally handled by the shell, which doesn't exist in this case.
  • Michael Burr
    Michael Burr about 12 years
    One thing to be aware of is that the debugger redirection was broken in VS2008: stackoverflow.com/a/3122074/12711 It's not clear exactly which versions of 2008 it's broken in - it's working for me today in VS 2008 SP1 on Win7 x64, but comments in the bug report indicates that for some people VS2008 SP1 doesn't fix it.
  • Ivan Gonzalez
    Ivan Gonzalez about 4 years
    This is so good for debuggers who don't allow piping like vscode