Open CMD in the directory where my program is - c++

I have a piece of software written in c++, that has to call command line and execute 2 simple commands. The problem is, they need to be executed in the main directory of my program (folder where exe file is). How can I make sure, that they will execute in this directory, if it can be different on PCs (for example "Program Files" or "Program Files(x86)").

On windows, you can use the GetModuleFileName WinAPI, which will return you the path of the exe that you can cd to and execute your commands.

Related

How to have a C++ program run a cmd command [duplicate]

I'm trying to use the system() function in a C program.
For example, I tried to create a directory on my desktop, using the system() function.My code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
system("cd c:\\Users\\USER\\Desktop");
system("mkdir test");
return 0;
}
When I run this code, a directory is created, but not on my desktop. It is created in my project directory.
Why is this happens?
Can I use the cd command in the system() function? If not, is there an replacement to the cd command that will work with system()?
I'm using Windows OS. I'm trying to use system() from a C program as I use cmd program.
I know that I can create the directory using WinAPI without any problem. I don't want to use WinAPI, my question is how can I make it work using system().
The changed directory only lasts for the duration of the system command. The command starts a separate program, which inherits its current directory from your program, but when that program exits its current directory dies with it.
You can use && to join the commands together, and it will work:
system("cd /D C:\\Users\\USER\\Desktop && mkdir test");
I also added the /D switch, or the CD command would not change drive letter if it were called from a different drive.
However, mkdir is perfectly capable of accepting a full path, so you could simply do:
system("mkdir C:\\Users\\USER\\Desktop\\test");
When you say system("some shell command");, the program spawns a shell to run the command. The shell has its own idea of the current directory, separate from your program's. The shell cds to the directory just as you asked it to, and then dies, leaving your process's CWD unaffected.
You could simply say _chdir("c:\\Users\\User\\Desktop"); to set the current directory before running the "mkdir" command. The shell that spawns to run it will then inherit your program's current directory and make the folder in the right place.
(For that matter, you could say _mkdir("test") as well, and stop using system unnecessarily. You should only reach for system when you're trying to do something that's worth running an external program / shell for.)
You have to perform both the commands on a single line like this,
system("cd c:\\Users\\USER\\Desktop && mkdir test");

How do I run a command line tool from a different directory in C++

I am trying to build a C++ application that makes a bunch of command line calls on the Windows command prompt using the system() function. The C++ runs in directory BSP below. BSP contains a sub-folder BSP/XST/Files. One of the commands the application makes, needs to call a command line tool (Xilinx Synthesis tools) that needs to run in the Files directory.
BSP
|---XST
|---|---Files
Doing it manually on the command prompt I would do something similar to:
>>cd XST
>>cd Files
>>xst -h
Is there a way to call a tool in the sub-directory from the BSP directory?
I looked at this question here, but it does not work. I'm guessing because they are talking about an executable that is stored within the sub-directory whereas I am calling a command line tool (i.e. uses environment variables).
To simplify: Is there a command/option to run a command line tool in a sub-folder on the Windows command prompt? I can just emulate the statement via my C++.
As suggested by #CodyGray in the comments, my idea was to use SetCurrentDirectory. If your program is located in the BST directory and you want to run xst in the sub-folder XST\Files relative to it, then it makes sense to also use GetModuleFileName. Use this function to retrieve the path to your program and then replace the file name by your sub-folder. Finally change directory to the modified path:
#include <string>
using namespace std;
int main()
{
// Get the path to your program.
char moduleFilePath[MAX_PATH];
GetModuleFileName(NULL, moduleFilePath, MAX_PATH);
// Find the backslash in front of the name of your program.
string::size_type pos = string(moduleFilePath).rfind("\\");
// Replace the program name by your sub-folder.
string subFolderPath = string(moduleFilePath).substr(0, pos) + "\\XST\\Files";
// Change into the sub-folder relative to your program.
SetCurrentDirectory(subFolderPath.c_str());
// Execute some program in your sub-folder.
system("type test.txt");
return 0;
}
As I don't have xst, I put a text file test.txt into the sub-folder for testing purposes. The file just contains Test test test, so the program above prints out the following:
Test test test
But as suggested by #MikeVine, CreateProcess might be a smarter solution.

Visual Studio 2010 run .exe from command line vs run (f5) debug

I am new to c++ and am making a very simple program. All my program does is call a function from the main function, which reads in a text file and returns. To check that I am reading in the file correctly, I am trying to print out the strings I have read in. The print out (cout) works properly when I run from Visual Studio (f5). However, when I run the executable from command line, none of the print outs from my function show up. Only print outs directly in the main function appear. I cannot find a similar question elsewhere. Any help would be appreciated.
When you run a program from within VC++ the current directory is set to the project directory by default, but the application is by default in a different folder.
E.g. the application may be:
D:\Work\MyApp\Debug\MyApp.exe
But the project directory may be:
D:\Work\MyApp\MyApp\
When you start the program from outside of VC++ you need to take steps to make sure the current directory is correct, or that the executable and any data files it refers to are in the same folder.
The default working directory for an IDE-launched project in Visual Studio is the project folder. This is the folder where you project file resides (the .vcproj or .vcprojx file is the project file).
If the data file you are reading is in the same folder, code like this:
std::ifstream inf("datafile.txt");
will succeed because the current working folder and the folder where the data file resides are the same.
However, if you switch to where the executable was written (typically this is the project-dir/Debug or project-dir/Release folders) and run the same executable from a command-shell, the data file will not be found.
To test this is the case. Do the following:
Open a command prompt.
Switch to the project folder where your data file resides.
Run the executable with a specified path: ./Debug/YourProgram.exe, for example.
Note: you can avoid this by having the program take the data file name as an argv[] parameter. Then your program will simply use whatever file you tell it to at launch-time.

Fortran Open from current directory

I am writing a fortran program and I would like to know if it is possible to open a file from the same directory where the program itself is placed.
I am using Ubuntu 12.04 BTW.
For example, if I put the compiled program at the directory "/home/username/foo" I would like the program to open the file "/home/username/foo/bar.txt" and write "Hello!" in it.
My minimal working example is the following:
program main
implicit none
open(unit=20,file="bar.txt",action="write")
WRITE(20,*) "Hello!"
close(20)
end program main
When I compile using gfortran it opens and writes in the file "/home/username/bar.txt" no matter where I put the program file.
On the other hand, when I compile it for windows (using mingw) making a .exe file and execute it in windows it does what I want, it opens the file where the executable file is placed.
[EDIT] I just found out that if I execute the program by double clicking it, it will open the file in the program directory but when I execute it from Terminal it opens at "/home/username/", so maybe is more about the way I send the command from Terminal, currently I am doing it by the following command "/home/username/foo/myprogram".
I too am running Ubuntu 12.04 with gfortran 4.6.3, but I do not experience this. Where ever it is that I place my executable, there is bar.txt after execution.
That said, if you want a file at a specific place, then declare a character string as follows:
character(26) :: filename
filename="/home/username/foo/bar.txt"
and then open the file as
open(unit=20, file=filename)
and you are home free.
EDIT
I just noticed your edit. I imagine that you open terminal and do not cd to the location of the executable, but run the command for execution. That would indeed cause you to always have the file open in whatever folder you are currently in.

Convert .bat to .exe without depending on cmd.exe

I have a bat to execute all files in a folder every 15 seconds. I need to make it .exe to run it as a hidden application.
I have converted it to .exe with bat to exe converter and other programs, and I see that in all of them, when I execute the .exe, for example, Load.exe, automatically start a cmd.exe at the same time. If I kill Load.exe it still continues to run, until I kill cmd.exe.
So, can I make an .exe from a .bat without depending on a cmd.exe?
Short but correct answer: no, you can not.
I don't know if it depends on CMD or not (probably it doesn't), but you can use C++ function system to execute each line of your .bat file.
Do it something like :
#include <stdlib.h>
int main()
{
system("<batch command goes here>");
return 0;
}
Simultaneously, you can add each line of .bat file to system function and execute it as a CPP/C program.