Not able read from input output files in Sublime Text 3 - c++

I have successfully installed MinGW and have tried running cpp programs from command prompt and it runs perfectly. In Sublime I tried using default build system 'C++ Single File', it prints simple statements in the console below but it cannot take input. After building the programs with 'cin' commands the program does not show any output and I try giving input in the console below and press ENTER but nothing happens. Then I found a method of using 'freopen' command to take input from a file and output to a different file. But the programs runs successfully but doesn't output anything in the output file. Same problem -- mingw64 + sublime 3 input output files not working
After this I tried many other build files I found on web but none of them work.
{
"cmd": ["g++.exe", "-std=c++17", "${file}",
"-o", "${file_base_name}.exe",
"&&", "${file_base_name}.exe<input.in>output.out"],
"shell":true,
"working_dir":"$file_path",
"selector":"source.cpp"
}
The output.out file is always empty. I have input and output files in same directly and I think it is reading input correctly because when I delete the input file it gives an error that it cannot find the input file. I even tried using text files (input.txt, output.txt) after moding build system code but it doesn't work.
I have many other build codes but none of them works.
I tried running g++.exe -std=c++17 contest1a.cpp -o contest1a.exe && contest1a.exe<input.in>output.out in command prompt but the output.out file is still empty.
I replaced first line with "cmd" : ["g++ -std=c++14 $file_name -o $file_base_name && timeout 4s $file_base_name < input.in > output.out"], but it gives error because I think .exe is not suffixed which I tried adding and program runs but output is still empty.
Why is 'freopen' not working, why is any build not able to write in output file ?

So it turns out Windows defender has a new feature 'Block Folder Access' which blocked my program from modifying the output file. I wasted one day solving this issue smh

Related

Sublime Text 3 intel oneAPI Fortran build system

Has anyone figured out how to write the build system for the oneAPI Fortran compiler?
Previously, i was using Parallel Studio XE ifort, and i managed to get it working using the solution here:
{
"cmd": ["cmd", "/e:on", "/v:on", "/k", "ipsxe-comp-vars intel64 vs2013 && ifort ${file}"],
"file_regex": "^.*\\\\([0-9A-Za-z_]+\\.[A-Za-z0-9]+)\\(([0-9]+)\\):[ ]+error[ ]+#([0-9]+):[ ]+(.*)$",
"working_dir":"${file_path}",
"selector":"source.f ,source.for ,source.ftn ,source.f90 ,source.fpp ,source.i ,source.i90",
"encoding":"cp936",
"path":"C:\\Program Files (x86)\\IntelSWTools\\compilers_and_libraries_2017.4.210\\windows\\bin;${path}",
"variants":
[
{
"name": "Run",
"cmd": ["cmd", "/e:on", "/v:on", "/c", "ipsxe-comp-vars intel64 vs2013 && ifort ${file} && ${file_base_name}"]
}
]
}
I tried changing the paths to the new ones but it doesn't work. I get the following error:
"ipsxe-comp-vars" is not recognized as an internal or external command,
program o executable.
I found the answer. Explanation below. Posting the working build system here for visibility.
This should be the build system:
{
"cmd": ["cmd", "/e:on", "/v:on", "/S", "/k", "C:\\\"Program Files (x86)\"\\Intel\\oneAPI\\setvars.bat intel64 vs2022 && ifort ${file}"],
"file_regex": "^.*\\\\([0-9A-Za-z_]+\\.[A-Za-z0-9]+)\\(([0-9]+)\\):[ ]+error[ ]+#([0-9]+):[ ]+(.*)$",
"working_dir":"${file_path}",
"selector":"source.f ,source.for ,source.ftn ,source.f90 ,source.fpp ,source.i ,source.i90",
"encoding":"cp936",
"path":"C:\\Program Files (x86)\\Intel\\oneAPI\\compiler\\latest\\windows\\bin\\intel64;${path}",
"variants":
[
{
"name": "Run",
"cmd": ["cmd", "/e:on", "/v:on", "/s", "/c", "C:\\\"Program Files (x86)\"\\Intel\\oneAPI\\setvars.bat intel64 vs2022 && ifort ${file} && ${file_base_name}"]
}
]
}
Why the problem happens
For starters, ipsxe-comp-vars is a batch file which when run, sets up environment variables required to execute the intel compilers. This file is specific to Intel Parallel Studio XE (IPSXE). Now, when installing IPSXE, it would add this batch file to your PATH, meaning you could simply call ipsxe-comp-vars from any directory to set up the required environment variables.
Intel oneAPI has a differently named file, that essentially does the same thing, called setvars.bat. This file is stored in:
C:\Program Files (x86)\Intel\oneAPI\setvars.bat
So, at first it seems that calling ipsxe-comp-vars fails because the file is named differently. However, unlike IPSXE did with ipsxe-comp-vars, oneAPI does not add setvars to PATH, so you cannot simply call setvars, you have to usethe full path.
How to solve it
With IPSXE, you could call ipsxe-comp-vars and it would run the batch file that sets up environment variables, but with oneAPI either you add the file to PATH (not reccomended because it has a generic name), or you use the full path when calling it (same as above):
C:\Program Files (x86)\Intel\oneAPI\setvars.bat
Now, because you have to plug this in into the build system config, you need to format it correctly. ST runs the commands in a cmd.exe, so you have to use the correct options and format the path in a way that cmd can understand it:
options (you can get a full list by opening a cmd prompt, typing cmd /? and hitting return):
- /e:on Enables command extensions
- /v:on Enables extension of environment variables
- /s Modifies how the string following a /c or /k is read
- /k Executes the string command and continues
The path to the setvars.bat file must be formatted as follows:
C:\\\"Program Files (x86)\"\\Intel\\oneAPI\\setvars.bat
Each \ separating dirs needs to be escaped (using \ as well)
needs to be enclosed in double quotes, since it contains a whitespace. Each double quote needs to escaped as well (once again with )
The following options are specific to the setvars.bat file:
- intel64 specifies 64-bit configuration
- vs2022 specifies Visual Studio 2022 as the developer cmd or
powershell version to use
Finally, ifort is called on the current file with ifort ${file}
Additionally, the build system is completed with a variant "Run". This variant runs the output file once it has been compiled(&& ${file_base_name}), and will show the output in the Sublime Text 3 console (does not accept inputs, if anyone knows how to setup up sublimeREPL for Fortran please tell me)

getting an access denied error while building a cpp file in sublime text

So i downloaded sublime text and installed mingw compiler. I have verified through cmd prompt that mingw is installed correctly. I added the environment path to my compiler in settings.
I created a new build system on sublime text:
{
"cmd": ["g++.exe","-std=c++17", "${file}", "-o", "${file_base_name}.exe", "&&" , "${file_base_name}.exe<inputf.in>outputf.in"],
"shell":true,
"working_dir":"$file_path",
"selector":"source.cpp"
}
Above system i took from this site: https://blog.codingblocks.com/2019/setting-up-a-c-competitive-programming-environment/
Basically it takes input from inputf.in and shows output at outputf.in
When i write my program of a simple printing of "hello world" and try to build using Cntrl + B, i get the following error.
Access is denied.
[Finished in 1.1s]
Any help would be appreciated. Thanku
EDIT: It was my antivirus. Thanku to all who answered. :D

Script for Notepad++ NppExec for C++ in ubuntu

I just switched to ubuntu and I wanted to setup notepad++ for CPP.
So I used the NppExec plugin to compile within notepad++,
My script was :
npp_save
g++ "$(FULL_CURRENT_PATH)" -o "$(CURRENT_DIRECTORY)\$(NAME_PART)obj"
./"$(NAME_PART)obj"
Here the "obj" I used is to just save the file with an "obj" keyword nothing else.
The last line ./"$(NAME_PART)obj" is to run the program.
But it looks not working in ubuntu, it produces this error:
NPP_SAVE: Z:\home\username\cpp\test.cpp
g++ "Z:\home\username\cpp\test.cpp" -o "Z:\home\username\cpp\testobj"
; about to start a child process: "g++ "Z:\home\username\cpp\test.cpp" -o "Z:\home\username\cpp\testobj"
CreatProcess() failed with error code 2:
File not found.
./"testobj"
; about to start a child process: "./"testobj""
CreatProcess() failed with error code 2:
File not found.
I have investigated some of what I think is the problem, so I think is the usage of / and \ in changing the directory.
I don't know how to fix that, so I can not be sure.
Any ideas? :) I am using vim btw in the same machine and it is working perfectly.
In theory it might be possible (see below), in practice it is rather convoluted and works only for simple compiles (like single file hello world type).
I would suggest you try a linux program, e.g.
an editor like
scite (same editing engine as notepad++) or
kate
or a real IDE like
kdeveloper or
qtcreator.
The problems with Notepad++ inside wine and g++ outside wine (from the linux install ) are this:
notepad++ inside wine under linux is still a windows program
NppExec can only do, what a cmd inside wine can do.
starting g++ directly inside cmd is an error due to g++ being a linux binary and not a windows binary
that is your CreatProcess() failed with error code 2, it means: you are trying to execute a linux program inside wine.
That does not work! (At least not so easy.)
Though you can start linux program inside cmd inside wine using start /unix ...
started this way, g++ wants linux paths and NppExec through its variables will provide only windows paths (whatever wine has set up as drives like Z:\home\username\src\hello.cpp)
though you can convert wine paths to linux paths via the winepath -u command.
g++ started through 'start /unix ... ' inside a cmd inside wine has no proper terminal to report errors to you
though you can start an xterm for g++ and have g++ reports its messages to the xterm
the downside is that g++ will report errors using the linux paths in the xterm, so you cannot double click on an error message an get to the corresponding filename and line.
You get the idea: its complicated not comfortable.
What worked for me for a helloword.cpp was this NppExec script:
NPP_SAVE
npp_run cmd /c start /unix /usr/bin/xterm -e "/usr/bin/winepath -u '$(FULL_CURRENT_PATH)' | xargs g++ -o /tmp/a.out && /tmp/a.out ; echo 'Press return'; read"
The second line
uses an xterm,
let winepath convert the Z:\home\... path to /home/... and
have that send to g++ for compilation using /tmp/a.out as binary
if compile is successfull, /tmp/a.out is executed
the echo and read are for keeping the xterm open so that you can read the output.
If you really want to use Notepad++ inside wine, one option might be using Gnu Make outside of wine and have NppExec run make all or make run similar to the g++ in my script example. That would work for more complicated compiles.

Sublime Text no build output

Using build option does not give any output except of
[Finished in 0.4s]
I didn't configure any c++.sublime-build file, I just installed C++ compiler and added it to the system path. It should work out of the box, but it is not. I also use Pawn language very often, but I had to configure my pawn.sublime-build file in order to work. There is a similar problem, after building if there are some errors it says:
[Finished in 0.4s]
But if the code has no errors, it says:
Welcome to the AMX Mod X 1.8.1-300 Compiler.
Copyright (c) 1997-2013 ITB CompuPhase, AMX Mod X Team
Header size: 836 bytes
Code size: 5048 bytes
Data size: 2148 bytes
Stack/heap size: 16384 bytes; estimated max. usage=883 cells (3532 bytes)
Total requirements: 24416 bytes
Done.
[Finished in 0.1s]
It would by nice if sublime could just show the output from compiler.
Here's my Pawn.sublime-build
{
"cmd": ["amxxpc.exe", "$file"],
// "cmd": ["compile.exe"],
// "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"file_regex": "(.*)\\((\\d+)\\)\\s*:\\s*()(.*)",
"selector": ["source.pwn", "source.pawn", "source.sma", "source.inc"],
"path": "Extensions\\pawn\\"
}
I tried diffrent versions of the "file_regex" and also without it, just like in tutorials, but it just doesn't show output if code has an error.
On the other hand, Python shows up an output, like this:
print"test")
^
SyntaxError: invalid syntax
[Finished in 0.2s with exit code 1]
[shell_cmd: python -u "C:\Users\Michał\OneDrive\Documents\test.py"]
[dir: C:\Users\Michał\OneDrive\Documents]
[path: C:\Program Files (x86)\ActiveState Komodo Edit 9\;C:\Python34\;C:\Python34\Scripts;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files (x86)\AMD APP\bin\x86_64;C:\Program Files (x86)\AMD APP\bin\x86;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\MinGW\bin]
And here's output with corrected code:
test
[Finished in 0.1s]
How can I get output from C++ and Pawn files?
What I am doing wrong?
I use MinGW for C++ and amxxpc.exe (AMX MOD X COMPILER) for pawn.
Thank you in advance for any solution and sorry for my english. I'm from Poland.
Open up Sublime Text 2 and go to Tools -> Build System -> New Build System...
Enter the following:
{
"cmd": ["pawncc.exe", "-i includes", "$file", "-;+"],
"path": "C:/path/to/pawno/folder"
}
Of course you need to replace the path
Save this file and close Sublime Text 2
Now navigate to AppData/Roaming/Sublime Text 2/Packages/User
Open up the .sublime-keymap file based on your operating system
Enter the following (this example is for the Windows file):
[
{ "keys": ["f5"], "command": "build" }
]
You can change f5 to another key if you wish
Save and reopen Sublime Text 2
Now go to Tools -> Build System
Click on the name of the build you created
`

Sublime Text 2 Run built file after building

So I'm new to this but I am using Sublime Text 2 on Mac OS X and the problem is that when i simply run my c++ code it runs in a local output tab and it dont allow me to do any input, so to input variables i need to build my code and then locate it in finder and open in terminal, but is there a way to allow input in that little output windows or simply tell ST2 to open the built file just after building it?
P.S.
I found the answear to my own question :D
I will add it so if someone needs it - it will be here.
so you need to eddit c++.sublime-build file - just add this
"cmd": ["bash", "-c", "g++ '${file}' -o '${file_path}/${file_base_name}' && open -a Terminal.app '${file_path}/${file_base_name}'"]
before
"variants":
and the when you build your programm it will just open it in terminal