Executing a cmd using "system()" doesn't look for path - c++

I'm trying to execute a graphviz/dot cmd from within my program,
but I'm still getting the error sh: dot: command not found.
So that's the line that matters:
system("dot -Tpng myfile.txt -o mypic.png");
Executing it on the terminal works pretty well, cause dot is set into my path variable.
Trying to execute the same programmatically ends up in the described error.
Is there any trick using the system cmd where I have to mention my path additionally?
(Btw: I already did include stdlib.h, dunno wether that's important...)
Thx a lot!

You can try something like this:
#include <stdlib.h>
int main()
{
system("PATH=$PATH:<YOUR PATH TO **dot**>;<**dot** and the rest>");
}
as shocking as it seems, this works for me ...
(obviously replace between < > ... )

Related

Multiple failed attempts to open a text file in C++

I have been spending the past hour trying to figure out how to display a text document programmatically in c++.
The following is the simple main.cpp code, in which I create a directory "Profiles", and another directory "TestProfile" inside. In Profiles/TestProfile I create a "TestFile" and write "Test data" into it.
#include <iostream>
#include <fstream>
#include <sys/stat.h>
#include "Profile.hpp"
#include <stdlib.h>
using namespace std;
int main(int argc, const char * argv[]) {
ofstream fs;
mkdir("/Users/dario/Desktop/Profiles", S_IRWXU);
mkdir("/Users/dario/Desktop/Profiles/TestProfile", S_IRWXU);
fs.open("/Users/dario/Desktop/Profiles/TestProfile/TestFile");
fs << "Test data\n";
string path = "/Users/dario/Desktop/Profiles/TestProfile/TestFile";
system(path.c_str());
return 0;
}
Everything up until line 30 works as expected. The directories are there, and "TestFile" is present with data written.
There are no errors present when compiling, but ./a.out gives me a permission denied
darios-mbp:FMES dario$ g++ -std=c++2a main.cpp
darios-mbp:FMES dario$ ./a.out
sh: /Users/dario/Desktop/Profiles/TestProfile/TestFile: Permission denied
I tried a few approaches.
First, I checked the permissions of the text file using ls -l,
it returned -rw-r--r--, so I used chmod 755. After that, the file permissions said -rwxr-xr-x. When running the same code, ./a.out returned no permission denied and it seems like it worked, but the text file was not opened.
Secondly, after the first approach didn't work, I deleted the directories and ./a.out to recreate the original permission denied issue.
I was getting permission denied as before, and this time I tried changing the ownership or the directory.
sh: /Users/dario/Desktop/Profiles/TestProfile/TestFile: Permission denied
darios-mbp:FMES dario$ chown -R $USER
/Users/dario/Desktop/Profiles/TestProfile/TestFile
darios-mbp:
The command worked, but running the code still did nothing despite no errors.
I tried chmod 755, but again no file was opened.
On previous attempts to fix this issue, after changing the ownership the same way I just explained, I even had an error where it was trying to run the first word of the textfile as a command
Line 1: Test: command not found
Although I was not able to recreate that while writing this.
Perhaps I am doing something completely wrong, but I looked as much as I could for solutions online before writing this post. None of them seemed to work. Perhaps my situation is unique in some way and someone here could help point me in the right direction. I apologize in advance if my post is in any way not to standard (if so, please let me know how I can improve questions for next time).h
I am running the code on xcode
EDIT
Thanks to #Codo for the answer, the following edit did what I was looking for
string path = "/Users/dario/Desktop/Profiles/TestProfile/TestFile";
string cmd = "open -a TextEdit ";
system(cmd.c_str());
I understand that you want to open the created file with an appropriate application. Let's assume the file is /dir/file.
To open a file from the command line / terminal, you would run:
open /dir/file
Or, if you want a specific applications:
open -a TextEdit /dir/file
Also see The macOS open Command.
If you just run:
/dir/file
you would execute the file. That's probably not what you intended. And execution is only possible if the file meets certain criteria like execution rights and in the case of a shell script an appropriate header.
In C++, the system() function executes a command that would be valid on the command line (in the terminal).
Thus, to fix your program, change the line with system() to:
string cmd = "open -a TextEdit ";
cmd += path;
system(cmd.c_str());
And for your next question: Paste code as text, not as image: Why not upload images of code/errors when asking a question?

Command line cpp compile isn't working properly

First of all, I made a simple program to test if compiling was working properly, it is as follows:
#include <iostream>
int main() {
std::cout << "Oi!";
}
Quite simple indeed, should compile properly, right? Wrong.
I am using MinGW for Windows 10, just to let you know.
I tried compiling it using "cpp <file name>.cpp -o a.exe" and was, first time, going through with the compile, but once I tried to execute the file it would send me this:
./a.exe: line 14: namespace: command not found
./a.exe: line 20: syntax error near unexpected token `('
./a.exe: line 20: ` typedef decltype(nullptr) nullptr_t;'
Clearly something was wrong, so I searched for a solution, found out someone had a similar problem and doing a reinstall solved it, so I went to the MinGW installation manager, noticed the C++ compiler library wasn't installed and installed it. It felt I was doing alright.
I also noticed that they were using g++ instead of cpp and tried that instead, it worked properly this time, but I would like to note that I have "git bash" installed, so I assume it used "git bash" 's command and not MinGW's.
So I opened cmd and tried using cpp, once I tried executing the program cmd said that the software wasn't compatible with the version of windows being executed, that doesn't sound right. I did a verbose compile with cpp and noticed this oddity:
#include <...> search starts here:
c:\mingw\bin\../lib/gcc/mingw32/6.3.0/include/c++
c:\mingw\bin\../lib/gcc/mingw32/6.3.0/include/c++/mingw32
c:\mingw\bin\../lib/gcc/mingw32/6.3.0/include/c++/backward
c:\mingw\bin\../lib/gcc/mingw32/6.3.0/include
c:\mingw\bin\../lib/gcc/mingw32/6.3.0/../../../../include
c:\mingw\bin\../lib/gcc/mingw32/6.3.0/include-fixed
End of search list.
That doesn't follow Windows's file path standard...
Does it have anything to do with the problems I am facing? I just want things to compile! ;w;
As mentioned above, you need to use g++ to compile your code. Try the command g++ --version If you get any result that doesn't include a version number you need to install g++. Here is a guide that may be helpful.
https://techsupportwhale.com/install-gcc-compiler-on-windows/

svm-scale: command not found

I am running a cpp code in xcode along with opencv. Inside the cpp code, there is a command line:
system("svm-scale -r allrange test_ind>> test_ind_scaled")
When i run the code, it's give a error like svm-scale: command not found
But when i run this command line (svm-scale -r allrange test_ind>> test_ind_scaled) from terminal, it's giving no error.
Any suggestion how to run this command line from inside the cpp code ?
Any suggestions would be greatly appreciated.
Try running svm-scale with its full path. Reading other xcode-related questions hinted that xcode might not use your PATH variable, so system doesn't know where to look for the specific command.

Can not run make command in Dev-cpp application

I have a very simple example named HelloWorld and located in folder "C:/HelloWorld" in Dev-Cpp as follows:
#include <iostream>
using namespace std;
int main(){
cout << "Hello World";
return 0;
}
Of course, if I press F11 (Compile and run) then the program executes normally.
But I want to execute this program in an another way: use make command!!!
I have known that the syntax to run make command : make -f [make file name]. In Dev-Cpp, this command is make -f "Makefile.win". So I jump into HelloWorld folder and type as follows:
The console displays many errors relating to included headers.
How can I run make command without any errors? Thanks in advance.
not so easy with make :)
at first if you use msvc or DevCpp try to find "Export makefile"
at second you need to configure your compilator(s).
at third you need to show what Makefile.win contains.
man:
http://www.gnu.org/software/make/manual/make.html
gcc:
https://gcc.gnu.org/install/binaries.html
All right. There are something wrong with my make command.
The problem is I had installed two versions of gcc in my computer. The HelloWorld example is compiled and built with the latest GCC version.
However, the make command I had type is belonged to the older GCC version. As a result, it leads to many errors.
In order to run make command correctly, I must to use the right make command that is defined in Dev-Cpp (below picture): mingw32-make.exe

Problem with running c program on mac?

I'm a total beginner in C programming so please bear with me. I have just started today and wanted to write a short program - well at least a small script that would just print out a line of text. Now here's what I did in order to achieve this:
I downloaded vim text editor and wrote this few lines of code:
#include <stdio.h>
int main(void)
{
printf("This is some text written in C \n");
return 0;
}
I saved it as inform.c and compiled it using "cc inform.c" command.
In the end I got a.out file but when I'm trying to run it says:
-bash: a.out: command not found
Can someone tell what I'm doing wrong here and point me in the right direction? Thanks.
Bash can't find your command because the current directory is not usually in the path.
Try:
$ ./a.out
It's a basic one.
on Mac, you need to specify were your executable is.
when you type a.out, the system look for the command in /usr/bin and other synstem binaries folders.
to be more precise type ./a.out
which basically says : "in this directory, command a.out"
you should also add directly the classical signature of main which is :
int main(int argc, char ** argv);