ive been trying to run c++ codes on sublime for a while but im having this problem "The system cannot find the file specified."
i tried googling for solutions but couldnt find any answer:
As you only specify input.txt without full path it's looking for it in the current folder the program is run: C:\Users\AboodPC\Desktop\Programs.
So either make sure C:\Users\AboodPC\Desktop\Programs\input.txt exists, or specify the full path in shell_cmd after the >.
Just create an input.txt file to take input into the program at run time.
In sublime text 3
Simply name your input file as "inputf.in"
And output file as "outputf.in"
And create new build system
Tool-->bulid system -->new build system
And paste below code
{"cmd": ["g++.exe","-std=c++14", "${file}", "-o", "${file_base_name}.exe", "&&" , "${file_base_name}.exe<inputf.in>outputf.in"],"selector":"source.cpp","shell":true,"working_dir":"$file_path"}
And save as name as"c++14.sublime-build"
And run the program ctrl+shift+B
Related
In my program I use multiple module files. In order to keep main folder clean I moved all of them in separate folders and made necessary changes to my shell script. Everything was done correctly as now I am able to compile and run the program. Unfortunately, Sublime Text 3 doesn't see .mod files located not in the main folder. On lines like this:
use mymodule
it says:
Fatal Error: Can't open module file ‘mymodule.mod’ for reading at (1): No such file or directory
How can I fix this?
In Sublime Text go to Preferences->Package Settings->SublimeLinter->Settings - User. There in the block "gfortranfixedform" or "gfortranmodern" (depends on what form you are using) add an argument: "args": ["-Ipath/to/modfiles"], where path/to/modfiles is a directory with your .mod files.
Maybe you have to change the line in config to this:
"shell_cmd": "gfortran '${file}' -Ipath/to/module -o '${file_path}/${file_base_name}'"
where path/to/module is a directory with your .mod file. (Just a suggestion, don't know anything about Sublime editor.)
I have made a simple scoring system which upon correct answer, stores the numbers of the player in the file.
I have used the file name like this :
ofstream outfile ("C:\Aadam\Desktop\Project\Scores.txt",ios::app);
But the problem with this approach is that what if I move the program over to a USB and try to run it in another computer. Now it will look in the directory I specified above but there is no Scores.txt file in there.
What I want to do is to give it a path which is in the project folder. So when I move the program, it shouldn't make a difference because I will move the whole project folder.
Of course I can do this :
ofstream outfile ("Scores.txt",ios::app)
which will always look in the project directory and it will work fine as long as I run the program from the IDE but what if I run the program from the .exe file which is two directories down like
"C:\Aadam\Desktop\Project\bin\Debug\Project.exe"
Now in this case, it can't open the file.
So if you know a good way to open files and kindly, Show me the Way.....
You can parse argv[0] (it will contain path used to invoke your executable - absolute or relative) and replace executable name in it with "Scores.txt"
The easiest way is to pass the file path to program as an argument.
When you run a program from IDE, the project directory is considered as current working directory. If the program is run from the command line, the current working directory is from where the command is being run.
If you run the exe file,ofstream outfile ("Scores.txt",ios::app) will create a file named "Scores.txt" in the same directory as your program.
I really prefer writing code in sublime text or anything else. So, naturally that's what I want to use. However, when I try to open the file in Netbeans, I get an error. So, I want to know how I can save a .cpp file from sublime text and then go about running it through the command prompt. I know I have to set up a path or something, but I'm not exactly sure how to do it. Thanks for any help at all. Also, I am new to C++ and programming in general(have dabbled in Python a bit).
EDIT: Really sorry, I meant how do I actually execute/run the file afterwards. Like if the program were to just print out "Hello World".
The following build system should suit your needs, assuming that you're using the GNU Compiler Collection and g++ for compiling your .cpp files:
{
"cmd": ["g++", "${file}", "-o", "${file_base_name}"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++",
"variants":
[
{
"name": "Run",
"cmd": ["${file_base_name}"]
}
]
}
Please note that the following instructions are for Sublime Text 2 only...
To use it, select Preferences -> Browse Packages... to open the Packages folder in Windows Explorer. It should be located in C:\Users\YourUserName\AppData\Roaming\Sublime Text 2. Or not, depending on your install. In either case, browse to the C++ directory and open the file C++.sublime-build in Sublime and set the syntax to JSON if you want it to look prettier. Replace its entire contents with the code above, then save the file. The old code is kind of convoluted, and also runs some commands needlessly.
Now, set the build system by going to Tools -> Build System and selecting Automatic. Assuming that g++ is in your PATH, you can build your executable using the CtrlB keyboard shortcut, also available via Tools -> Build. If your binary has already been compiled, you can run it by pressing CtrlShiftB.
One final note: if your program asks for any kind of input, or has a graphical user interface, this Run command won't work. Instead, replace it with the following:
"name": "Run",
"cmd": ["start", "cmd", "/k", "${file_path}/${file_base_name}"],
"shell": true
This will open a new instance of the command line and run your program from there, instead of inside Sublime. The /k switch means that the window will be kept open after your program has run, so you can examine output, errors, etc. If instead you want the window to close immediately, simply change the /k to /c.
Good luck!
I've been playing a bit with ST2 and it seems like a pretty cute editor. Unfortunatelly, its documentation is horrible.
And I'm being nice. So here's my question.
I have five files in a directory, which I usually build via a .bat file with
ifort file1.f90 file2.f90 file3.f90 ...
how can I define and execute this line on windows cmd (taking account the enviromental variables like PATH) from ST2 via a shortcut and see the output? Is something like that even possible at this stage with ST2?
I just made a new build like this:
{
"cmd": ["$file"],
"selector": "source.dosbatch"
}
Then you would put your ifort ... string in a .bat file and "build" that.
Here's a step by step way to run a batch file as part of your build process:
In Sublime Text 2 go to Tools -> Build System -> New Build System
You'll be presented with a new text file with the following code in it:
{
"cmd": ["make"]
}
Now change the "make" to the exact path of your batch file so it looks something like this:
{
"cmd": ["D:\\xampp\\htdocs\\myproject\\dostuff.bat"]
}
Notice you must use double backslash for windows paths.
Then save this file out as myproject.sublime-build in the default directory it asks you to save it in (should be $HOME_DIR/AppData/Roaming/Sublime Text 2/Packages/User).
Then in your project go back to Tools -> Build System and select the build system that is the same name as the file you just created (in this case it's "myproject").
Hit ctrl + b or go to tools -> build and your batch file should run and output will appear in the ST2 console.
This worked for me and works with paths and files with whitespaces.
I have fixed a bug which I've posted on the ST forum here and this here includes that fix.
Paste this into your Batch.sublime-build file.
This will run cmd.exe and run the code in its native console. This will accept your inputs of the batch file.
{
"file_patterns": ["*.bat", "*.cmd"],
"selector": "source.batch",
// This runs the batch file in the cmd window.
"shell_cmd": "start \"${file_name}\" call \"${file}\""
}
Here's a build that can be saved as BatchStConsole.sublime-build
This will run the code in Sublime Texts' console. This will not accept your inputs of the batch file. But still useful for debugging as it passes any arguments like the native CLI but just no interaction.
{
"file_patterns": ["*.bat", "*.cmd"],
"selector": "source.Batch",
// This outputs to Sublime Texts' console
"shell_cmd": "\"${file}\""
}
Also, in a new file ...\Data\Packages\User\Batch File.sublime-settings you can then place this code and save. This will build these filetypes when you have automatic build as your build detection.
{ "extensions": [ "bat", "cmd" ] }
Relevant help:
https://ss64.com/nt/start.html
https://docs.sublimetext.io/guide/usage/build-systems.html
https://www.sublimetext.com/docs/3/build_systems.html
I'm teaching myself some Django tonight using the local dev package at instantdjango.com
I made it through half the tutorial, building a basic map with points on it, then closed out of the console for a bit. I just fired it back up and now when I try to use notepad++ to edit files, console tells me it doesn't recognize the command. What happened?
When I as working through the first half, I was able to type: "notepad++ filename.ext" and I'd get a text editor that would pop up.
Now when I type that, it doesn't recognize the command.
How do I get back the ability to use the text editor and how did I lose it?
In a windows terminal, you can launch notepad++ with the following:
start notepad++ <filename>
Note that the filename is optional.
Most likely the directory in which the Notepad++ executable resides is not in your system's PATH. For information about fixing this please see How to set the path in Windows 2000 / Windows XP.
If you are using gitbash or cygwin, you can create an alias
alias np='start notepad++'
And use
np myfile.txt
This is what I have done, in this way you dont have to type notepad++
Create np.bat file with this set of commands
#echo off
start "" "C:\Program Files (x86)\Notepad++\notepad++.exe" %*
place np.bat file in c:\Windows
open the command prompt and type np or np myfile.txt and enter.
One way is to make a change to this registry key:
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\Notepad++.exe]
You can download a zipped .reg file from Donn Felker that lets you open a file with just an n shortcut.
I edited the .reg before running it and to make sure the path to Notepad++ is correct (e.g. C:\Program Files (x86)) and I also changed the shortcut to n instead of n.
Then double click to add to your registry.