'Auto input in VS code terminal from input.txt whenever compiled
Generally we need to type the input after running any file where we have std::cin, like the c++ code in below
int M,N;
cin>>M>>N;
int i,a[M],b[N];
for(i=0;i<M;i++)
{
cin>>a[i];
}
for(i=0;i<N;i++)
{
cin>>b[i];
}
Solution ob;
cout<<ob.countPairs(a, b, M, N)<<endl;
I just don't like to enter the same big 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. Ofc 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 ubuntu and run code in terminal using coderunner extension.
Solution 1:[1]
Scripting
Write your long input into a file, input.txt:
the quick brown fox jumped over the lazy dog
Use a bash script, script.sh:
# Compile your program, ie:
clang++ source.cpp -o application
# Check compilation succeeded
if [[ $? -ne 0 ]]; then
echo "compilation failed"
exit 1
fi
# Pipe your input into the application
cat input.txt | ./application
Finally, invoke your script:
$ bash script.sh
To Read:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Inigo Selwood |
