C++ "system(command)" not working on NetBeans / Windows - c++

I am working on a C++ project using CodeBlocks on Windows but then decided to switch to NetBeans IDE 8.2.
Within my project, I am calling another executable file with some passed parameters (I run the other .exe with suitable parameters then I take the output of it to use in my main project), and it used to work on CodeBlocks but not on NetBeans.
The code is the following:
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <cstdlib>
#include <string.h>
#include <sstream>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "My_Constants.h"
#include "Data.h"
#include "Parameters.h"
#include "Pattern.h"
#include "Squish.h"
#include "Deserializer.h"
#include "Automatic_Run.h"
using namespace std;
int main()
{
Parameters parameters;
parameters.mode = SQUISH;
Automatic_Run runner;
string inputname;
//--------------------------------User Input-------------------------------------
cout << "enter input file name \n";
getline(cin, inputname);
parameters.inputFileName.assign(inputname);
cout<<"=============================================================================\n";
//--------------------------------Running SQUISH/first call--------------------------
cout<<"Running SQUISH - first call\n";
char command[1000]="";
string passedParameters = " -i "+parameters.inputFileName +" -f "+ "t";
strcat(command,"C:\\Users\\Administrator\\Documents\\CodeBlocksProjects\\TestSQUISH\\bin\\Debug\\TestSQUISH.exe ");
strcat(command,passedParameters.c_str());
int result = system(command);
// the rest of the code(not relevant to the problem)
return 0;
}
On CodeBlocks, it used to work perfectly and give me the output as a file in the path of my main project (the one I am calling TestSQUISH from). However, now on NetBeans, I am getting the following error:
sh: C:UsersAdministratorDocumentsCodeBlocksProjectsTestSQUISHbinDebugTestSQUISH.exe: command not found
I checked the terminal of NetBeans to get an idea of what is happening (assuming it might be related) and I noticed that I have to change the path first, then run the executable using:
./ TestSQUISH.exe (+parameters)
However, that also didn't work for my project.
Can anyone please suggest a solution or tell me how I can make NetBeans run the command on a Windows terminal as CodeBlocks used to do?

Go to the project settings and set the project path for execution to be the folder where the other application is.
OR
Set the system path to include that folder.

Thanks to the comment of #Yksisarvinen, I was able to solve the problem.
Noticing that NetBeans uses the shell and not the Windows-style commands and after using the NetBeans own terminal to really get a clear idea of how it translates paths, I was able to run the code successfully using the following:
char command[1000]="";
string passedParameters = " -i "+parameters.inputFileName +" -f "+ "t";
strcat(command, "/cygdrive/c/Users/Administrator/Documents/CodeBlocksProjects/TestSQUISH/bin/Debug/TestSQUISH.exe ");
strcat(command,passedParameters.c_str());
int result = system(command);
Netbeans terminal adds cygdrive to the beginning of the path, and uses c instead of C:.
And in case the executable is in the same directory as your own project then this would be enough:
char command[1000]="";
string passedParameters = " -i "+parameters.inputFileName +" -f "+ "t";
strcat(command ,"./TestSQUISH.exe ");
strcat(command,passedParameters.c_str());
int result = system(command);

Related

How do I execute an existing binary that's in the same location as the main cpp file?

I'm making a program that depends heavily on another C binary. Since I don't feel like learning how to use headers and what not yet, I wanted to take the simple rout and just run a pre-compiled binary from the same folder in my cpp program.
Right now, my folder is setup like this: It has main.cpp, CMakeLists.txt, and the ibootim binary. Inside of main.cpp, how would I call ibootim?
From coding in python, it's taught me that I should be able to run
system("./ibootim");
but that doesn't work. Terminal tells me that there's no file found. Obvioiusly if I were to put the entire path to that binary, it would work. However, if other users were to download this, it would not work for them since they don't have the same computer, username, etc. as I do.
So my first question, my primary concern would be:
How do you run another binary that's in the same directory in a c++ program?
If this isn't possible for some reason, then I can try downloading ibootim from source and maybe using the header file:
How do you execute code from a C header in a C++ program?
For Windows, you can use GetModuleFileNameW() to get the absolute path to the running exe even if the working directory is different from the exe's directory. Then, you can use PathRemoveFileSpecW() to remove the filename from the path to get the exe's directory path. Then, you can use ShellExecuteW() to launch the exe with the filename you want while telling the function what directory to look in for the exe.
Here's a command-line example:
#include <windows.h>
#include <shlwapi.h>
#include <iostream>
#include <string>
#include <cwchar>
#include <cstdlib>
#include <cstdint>
#include <stdexcept>
#include <clocale>
using namespace std;
wstring get_exe_dir() {
wchar_t buffer[65537] = {L'\0'};
if (!GetModuleFileNameW(NULL, buffer, sizeof(buffer))) {
MessageBox(NULL, "GetModuleFileNameW length error", "EXE path is too long!", MB_OK);
throw length_error("");
}
wcout << buffer << L'\n';
PathRemoveFileSpecW(buffer);
wcout << buffer << L'\n';
return buffer;
}
int main() {
setlocale(LC_CTYPE, ".OCP");
const wstring exe_dir = get_exe_dir();
const intptr_t result = reinterpret_cast<intptr_t>(ShellExecuteW(NULL, L"open", L"\"other.exe\"", NULL, exe_dir.c_str(), SW_SHOWNORMAL));
if (result < 33) {
MessageBox(NULL, "Error launching other.exe", "Launch error", MB_OK);
return EXIT_FAILURE;
}
}
// g++ -Wall -Wextra exe_path.cc -o exe_path -O3 -s -lshlwapi
Maybe Mac has a similar function. I see _NSGetExecutablePath(). For shellExecute(), I see this answer that might help. But, perhaps system() is fine on Mac where it doesn't spawn another terminal window like it does on Windows.
In c++ if you want to use a binary you can use std::system() function.
But to do this the binary must be on the PATH.
If your binary is not on the path you can do something like this.
#include <iostream>
int main(){
#if _WIN32
std::system("./mybinarie.exe");
#else
std::system("./mybinarie");
#endif
return 0;
}
Starting the shell with std::system will ensure that you are in your working folder and that if the binary is in the working folder it should work.

Cannot create new text files by C++ in Codeblocks in Mac

I have a C++ code in Codeblocks in Mac OS that should create some .txt files and write something on them. It worked on Windows, however, it does not run correctly on Mac. It does not make any errors, and it announces that the procedure is terminated with status 0; however, it does not create any .txt files!! What can be the problem?
It even does not claim that Codeblocks needs special permission. Moreover, the execution window background is White; different from the usual execution window of Codeblocks, which is Black.
This is the code:
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <ctime>
using namespace std;
int main(){
for (int i=1;i<=1;i++){
ofstream fout ("test1.txt");
int n=rand()%10+1;
// int n=15;
fout<<n<<endl;
}
return 0;
}
This code just does not create any .txt file in Mac, while it worked on Windows.
"test1.txt",this is a relative path, you maybe hava use clion,you must know where relative path, but it is using a absolute path.

Is it possible to output a variable as a cmd command from a c++ program

Im making a simple command line program just to get my C++ knowledge to a basic level,
I would like to send a command from the C++ program to CMD that is made up of part of a command and the rest a variable here is my code so far:
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
string directory;
cout<<"Input the directory you would like to make your folder in: ";
cin>> directory;
system("mkdir" directory);
}
I get the error:
error: expected ')' before 'directory'|
Does anyone know a way of doing this? In the end I would like CMD to execute the command "mkdir C:*Inputted Directory*
I have hacked around with it trying to work it out, but to no success, I also have had a look on the internet but to no avail, thanks in advance.
Rather than call out to the shell use the _mkdir (for Windows) or mkdir (for Linux) function instead:
_mkdir(directory.c_str());
For Windows you'll need #include <direct.h>, for Linux you'll need #include <sys/stat.h> and #include <sys/types.h>

mpf_set_d causes an illegal instruction in c++

I'm using GMP and getting an illegal instruction that I have found to be caused by mpf_set_d. I'm programming in Netbeans with cygwin in c++.
Thanks in advance!
Edit:
I have a folder containing the following files:
.dep.inc, cyggcc_s-seh-1.dll, cyggmp-10.dll, cyggmpxx-4.dll, cygstdc++-6.dll, cygwin1.dll, my executable, gmp.h, gmpxx.h, libgmp.a, libgmp.la, libgmp.lai, libgmp.lai, libgmp.libcmd, libgmpxx.a, libgmpxx.la, libgmpxx.lai, main.cpp, main.o, main.o.d, Makefile, text file needed for program and folders needed for program.
I have tried many things, one of which was to add an executable to this folder that ran the following code:
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include "gmpxx.h"
#include <stdarg.h>
#include <string>
using namespace std;
int main()
{
mpf_t a;
mpf_init(a);
mpf_set_d(a,3.1415926535);
cout << "works" << endl;
}
Running this on my own computer with GMP installed gives me 'works' after which it closes, on another computer that does not have GMP installed it throws an error and closes. The error is an exception: status_illegal_instruction.
I can't give you my code but I can post snippets. Please tell me if more information is necessary!
Thanks again.
Edit 2:
The same counts for mpf_set_str as well as the c++ wrapper.

Error using wget in c++

I downloaded wget from gnuwin32 and I am trying to run the command in a c++ program using the system() function. I am using visual studio 2012 on a windows OS as my compiler. wget runs on the command line but does not run when I put it in the system function. My error is " 'wget' is not recognized as an internal or external command,operable program or batch file"
Here is my code:
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = string("wget -O test.csv \"http://")+"somewebsitelink\"";
const char *x = str.c_str();
cout << str << endl;
system(x);
system("pause");
return 0;
}
Visual Studio is probably overriding your normal path somehow if it really is in the path and not working.
Just put the full path in manually:
system("c:/path/wget ...");
wget needs have its directory in the list of directories in your PATH environment variable.
Since you are using Windows, you'll have to modify it your user configuration. It has been years since I have used windows, so I no longer recall exactly where it is.