0

Generally we need to type the input after running any file where we have std::cin, like the C++ code in below

#include <iostream>
using namespace std;
int main()
{
    // your code goes here
    int t;
    cin >> t;
}

I just don't like to enter the same input every time. So I want to automate this process for the same input, say I save my input in a file called input.txt and after running the file, it should take the input from input.txt and output the results. Of course saving input to the clipboard is one way but I might want to copy other things while coding and copy-pasting is itself is again one small job.

I use VS code editor in windows and run code in terminal extension.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
idfcnight
  • 1
  • 2
  • One option if you don't want to take input from the console is to not use the feature designed to take input from the console. For instance, look up how to read from a file. – TheUndeadFish Feb 05 '23 at 15:31
  • 1
    Off-topic: About [`using namespace std`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)... – Aconcagua Feb 05 '23 at 15:31
  • Well right now i don't have much of an option since it is part of my course and I need to do it to pass. but it is something would help in future so I'd love to be able to deal with it effeciently. – idfcnight Feb 05 '23 at 15:33
  • Semi-related: [Use input (stdin) in debug console VScode](https://stackoverflow.com/questions/64786161/use-input-stdin-in-debug-console-vscode) depending on how you're running your program it might not work. launch.json is normally used when debugging. – JohnFilleau Feb 05 '23 at 15:42

2 Answers2

1

The solution is to learn to use your shell. I’m assuming a Unix-like shell here.

Type all of your inputs into a file as you would enter them while the program is running. Save it.

When you run your program, use the command a.out < input.txt. Substitute the appropriate names, obviously.

Your program will read the inputs from the file as if they had been typed in.

Note that because nothing was actually typed in, your formatting might look a bit off, but it’s not a big deal compared to the time you’re saving in running your tests.

sweenish
  • 4,793
  • 3
  • 12
  • 23
-1

You could use, following function inside your main function

int main() {
freopen('input.txt', 'r', stdin);
freopen('output.txt', 'w', stdout); //if you want to save output to a file.
/*
... your code 
*/
}
  • I'm not a fan of this because it suggests modifying a program to be able to run certain tests. Once you're done developing, you have to remove this code. The result is that the program you release isn't really the program you tested. I *am* a fan of this answer because sometimes you need to be pragmatic and just test the damn thing. For something this simple it doesn't really matter, but I'm still against this answer because of the bad precedent it sets. – JohnFilleau Feb 05 '23 at 16:06
  • I understand what you are saying, there is difference between the testing when you read the input from file within the program, rather then console. So, its best way to pipe the file into slandered input of program. Thanks for clarification, otherwise I would have ignored this point. – Lokesh Kurre Feb 05 '23 at 16:15