Search code examples
cgdb

Debugging in GDB: Is there a way to have inputs automated from a separate file?


For example, when the user is prompted with an input from a scanf(), is there a way to, say, have a text file with inputs that can be read as user inputs instead of having to type in the inputs every time?

I am trying to make a sorted linked list that is being sorted as the user inputs the integer. The way I have it set up is the user selects an option from a menu, and if they pick adding another integer to the list, they input another integer. I tried making a text file with one integer on every line (first to pick from the menu, and second to pick the number they want in the list) and used set args inputs.txt, but it looks like it didn't work.


Solution

  • You can use input redirection:

    Make a text a file of ordered inputs just as you would type inputs in and execute the file with

    ./a.out < inputs.txt

    Depending if you are on windows or unix machines the syntax could be a little different, if I rememeber Windows vscode used something a little different but its only a google search away.

    Edit: found an old file I worked on using Powershell through VsCode, here's how I redirected if your not on Unix.

    Get-Content input.txt | ./program < output.txt.

    also used:

    gcc array.c stats.c

    get-content input.txt | ./a.exe

    These are old programs so I honestly don't remember the exact contents and functions of these files but I do know this is how I redirected an automated test input.