Auto input in VS code terminal from input.txt whenever compiled - c++

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.

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:
Piping input to applications
Return values in bash

Related

How to quickly write same thing to cin every time program is run?

I'm currently using C++ and VSCode for competitive programming in USACO. Recently, they changed their input format from file input to inputting data from terminal/stdin, which is slower because I have to paste the input into the terminal every time. Is there a way to just write data to a file and have it inputted into cin every time I run my program?
I personally use custom run configuration < input > myoutput && diff myoutput output with CodeRunner to read input and compare with expected output.
For example in case of C++,
In VSCode's settings.json, modify the code-runner.executorMap as below and use Ctrl+Alt+N to run
"code-runner.executorMap": {
...
"cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt < input > myoutput && diff myoutput output",
...
}
input - input to your program
output - the expected output from your program
myoutput - the actual output of your program.

Want to run a cpp executable in a new terminal and then send a file into the input stream

I would like to run a c++ executable in a new linux terminal, which I am doing using:
xterm -e executable options &disown
and this works. However, I also need to parse a text file through the command line. Normally, the file would be parsed by:
./executable options < inputFile.txt
and then the file is handled by the c++ code using this function:
void parse_lines(istream &in){
verify_version_number(in);
read_variables(in);
...
}
However the following line does not work:
xterm -e executable options < inputFile.txt &disown
How can I run the executable in a new terminal and then send the contents of inputFile.txt into the istream?
Thanks!
If you put quotes around the command, it will be sent to the shell as a single command, and the special characters < and & will be interpreted in the shell running within xterm rather than in the shell where you start xterm:
xterm -e "executable options < inputFile.txt &disown"
Try x-terminal-emulator instead of xterm. Perhaps it works.

Using grep with execl()

A little context for my project: We have an arbitrary number of files that need a separate process for each file then need to search using an exec() call to find every time a specific KEY is used. I know how to use grep from the command line using this command:
grep -o KEY FILENAME.txt | wc -l > OUTPUT.txt
But I cannot figure out how to do this in c++. I found a thread on here that gave me this line.
execl("/bin/grep","grep",pattern,filename,NULL);
It compiles and runs so I think it works but the problem is I need to output the number of times the pattern occurred to a file and I tried the line below but expectedly it didn't work. It gave this error "grep: out.txt: No such file or directory"
execl("/bin/grep", "grep",pattern,fileName,output,NULL);
Here are the directions of this part of my project.
You can do this by means of the
system call exec() , providing it with the path to the executable of the shell (typically, /bin/sh )
and, as arguments of /bin/sh , the string -c and the string corresponding to the search command
( grep -o ... ).
Some guidance here would be much appreciated!
For the actual execution as you would do on command line would be:
execl("/bin/sh", "/bin/sh", "-c", "grep -o KEY FILENAME.txt | wc -l > OUTPUT.txt")
This will mean that the shell would take the line grep -o KEY FILENAME.txt | wc -l > OUTPUT.txt, interpret it and run it. Note that this will include wild card expansion and all what the shell does.
Then of course if you wan't to continue after it has completed you will have to fork first because execl does not return if it's successful at starting the program (ie bash).

Running C++ program from ruby

How can you run a C++ program from a Ruby script?
Suppose that the Ruby script generates a file, called "hello.txt" and I want to run a C++ program to take the hello.txt file, work with it and write another file, called "result.txt" and the Ruby script continues to read the result.txt file.
For example, in the Linux shell I wrote g++ hello.c -c hello -o hello to receive the "result.txt" file.
Is there is a way that I can run the shell code from a Ruby program?
You can use system like other people said, however you should also check the exit value to verify the success or failure.
r = system("g++ hello.c -c hello -o hello") #=> r = true if success, nil if failed
You can use system :
system("./hello file.txt")
I find that backticks are more succinct than system.
You can trigger the C++ program by shelling out as follows:
`./hello file.txt`
Can you clarify whether you need to read results.txt from the current directory?
If so, you could use something like contents = IO::readlines './results.txt'

Dos create a batch file and run with multiple C++ programs

Respected sirs,
My name is #nimit. I want to create a batch file and run it in a DOS prompt. The batch file will execute a C++ program I've written. The output should be stored in a single text-file. How can I do this?
The C++ program output should be stored in a particular text file.
Thanks in advance,
#nimit
You can do this:
programname > outputgoeshere.txt
To collect outputs:
programname1 >> outputgoeshere.txt
programname2 >> outputgoeshere.txt
programname3 >> outputgoeshere.txt
Shell scripting (Batch files are a form of that) is something that every programmer should know how to do. I found a really great book on it a few years ago: Unix Shell Programming by Stephen Kochan and Patrick Wood. Granted, it's Unix -- and bash is far more powerful than DOS, but the principles are the same. Windows is picking up a lot of the tools that bash offers with powershell.
For a great website that lists out all of the CMD programs, visit http:// ss64.com/nt/ . That site also lists comparable bash and powershell commands. I also like how he shows you how to implement pseudo-functions, command line parameters, and all manner of cool things in batch files: http://ss64.com/nt/syntax.html
Good luck!
The following will redirect program output (stdout) to a file (overwrite file or create it if it does not exist)
$ command-name > output.log
The following will redirect program output (stdout) to a file (append file or create it if it does not exist)
$ command-name >> output.log
$ command-name >> output.log
The following will redirect program error message to a file called error.log:
$ command-name 2> error.log
Redirecting the standard error (stderr) and stdout to file, Use the following syntax:
$ command-name &> output_error.log