Note
- Programming Languages Covered - C++, Java, Python
- Code Editor used - VSCode
- Operating Systems covered - Windows, Linux, MacOS
Target
Create an environment such that -
- We could write 'input' inside a text-file
- Click on a icon
- And 'output' of the code will be shown in Terminal or will be written in a text-file
1. Create Folder
Create a new folder on desktop and open this folder inside VSCode
Create
app.cpp
,app.java
,app.py
in the folderAlso create
input.txt
in the folder with following text:
This is line 1
This is line 2
Download code-runner extension for VSCode and restart VSCode
Go to settings of VSCode with
Cntr + ,
Click on top-right 'file-icon' (to open settings in JSON)
Set
"code-runner.saveFileBeforeRun"
astrue
Keep this sfile open for further modification
2. C++ Setup
- Download and install MinGW-w64 for Windows - compiler for C++
Execute
g++ --version
inside PowerShell to check if compiler is correctly installedPaste following code inside
app.cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
string word1, word2; // memory-space for words
cin >> word1 >> word2; // insert first-word => word1 && second-word => word2
cout << word1 << " " << word2; // output "<word1> <word2>"
return 0;
}
- If yo are using Windows, Open PowerShell using
Cntr + ~
(Choose 'PowerShell' as Default shell if not already) and run the following command
g++ app.cpp -o app; Get-Content input.txt | .\app # write output in the powershell-termainal
# g++ app.cpp => compile 'app.cpp' with g++
# -o app => defining output file-name as 'app.exe'
# ; => end of command
# Get-Content input.txt | .\app => Execute 'app.exe' with content from 'input.txt'
# OR
g++ app.cpp -o app; Get-Content input.txt | .\app > output.txt # write output inside 'output.txt' file
OR
- If your are using Mac or Linux, Open Bash/Terminal using
Ctrl + ~
and run the following command
g++ app.cpp -o app && cat input.txt | ./app > output.txt
3. Java Setup
- Download and Install Java - for compilation of Java Code
Execute
java --version
inside PowerShell to check if compiler is installed correctlyPaste following code inside
app.java
file
import java.util.*;
import java.lang.*;
import java.io.*;
/* IMPORTANT: class-name must be equal to file-name, here file-name is 'app.java', hence class-name is 'class app' */
class app
{
public static void main (String[] args) throws java.lang.Exception
{
File input = new File("./input.txt"); // declare input
Scanner scanner1 = new Scanner(input); // declare scanner
String word1 = scanner1.next(); // scan first-word
String word2 = scanner1.next(); // scan second-word
String word3 = word1 + " " + word2;
// String line1 = sc1.nextLine(); // in-case if you want to scan complete line
System.out.print(word3); // output "<word1> <word2>"
}
}
- For Windows, open PowerShell and run command as -
javac app.java; java app # output in the powershell-terminal
# OR
javac app.java; java app > output.txt # write output inside 'output.txt'
# javac app.java => compile '.java' file using javac into bytecode as '.class'
# ; => end of previous-command
# > => transfer output genereated to the a file
# java app => execute '.class' file
- For Linux or MacOS, Open Bash and run command as -
javac app.java && java app # write output in bash-terminal
#OR
javac app.java && java app > output.txt # write output in output.txt file
# javac app.java => compile app.java to app.class
# && => and, bridge between two-separate commands
# java app => execute '.class' file
4. Python Setup
Download and Install - Python for compilation of python code
Execute
python --version
inside PowerShell to check if Python is correctly installedPaste following code into
app.py
line1 = input() # get 'first-line' as value of variable 'line1'
line2 = input() # get 'second-line' as value of variable 'line2'
arr1 = line1.split() # get array of individual words from 'line1'
print(arr1[0] + ' ' + arr1[1]) # output => "<word1> <word2>"
- For Windows, open PowerShell and run -
Get-Content .\input.txt | python .\app.py # output will print inside 'powershell'
Get-Content .\input.txt | python .\app.py > output.txt # ouput will be written inside 'output.txt'
- For Linux or MacOS, open Bash and run -
cat input.txt | python app.py # write output insided terminal
cat input.txt | python app.py > output.txt # write output inside file named - output.txt in the same folder
5. Execute PowerShell/Bash Commands with One-Click
- Open VSCode settings (inside JSON format) as described in the Step-1
- Comment out code for
cpp
,java
andpython
inside"code-runner.executorMap": {}
and add the json-code given below:
- For WIndows
"cpp": "g++ $fileName -o $fileNameWithoutExt; Get-Content input.txt | .\\$fileNameWithoutExt",
"java": "javac $fileName; java $fileNameWithoutExt",
"python": "Get-Content .\\input.txt | python .\\app.py",
- For Linux or Bash
"cpp": "g++ $fileName -o $fileNameWithoutExt && cat input.txt | ./$fileNameWithoutExt",
"java": "javac $fileName && java $fileNameWithoutExt",
"python": "cat input.txt | python app.py",
- Now come to the file
app.cpp
orapp.java
orapp.py
file - Click on the 'horizontal-rectangle' icon on 'top-left' => Now you can see the 'output' of your code inside PowerShell/Bash terminal
In this way you can test your code with just 'one-click' (with writing input inside 'input.txt' file) before submitting on 'Competitive Programming Platform'.
If you find any problems or want to suggest improvements, please let me know in comments and I will modify this article.
Top comments (2)
For anyone getting the error 'Get-Content' not recognised.
The Code runner is executing the command in CMD instead of Powershell, so go into the extension's settings and tick the option where it says run the commands in terminal.
Thanks buddy! :)