C++ system() not giving output of dsget command - c++

This is weird... Am on Windows 7 in a domain environment, doing a quick program to process a bunch of data [so no lecturing me on the evil of using system() =P ], and I've got this code:
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
system("dsget group \"CN=Accounting,OU=Groups,OU=Exchange Users,DC=MyDomain,DC=com\" -members");
}
The output is nothing at all. Stuff I've tried:
Ran that command verbatim on a command prompt, successfully got data back.
Did a "cout" on that command string to make sure the \" part was being processed right.
Did system("ipconfig -all") and other system commands to make sure that was working.
Did system("echo [dsget_command] > runThis.cmd") then system("runThis.cmd")... the only output was seeing it try to execute the dsget statement but still no results.
It's so weird that this is just happening to me on the "dsget" command, and also so weird that the command runs fine on a command prompt [not through the C++ program]. Any suggestions?

Related

Notepad++ NppExec console warning, need explanation "C++"

I've tried using Notepad++ to code c++ and followed a few tutorials on youtube, here's what I did:
-Installed gcc/g++ compiler using mingw64
-Installed NppExec plugin on N++
-Typed in the following compilier script and saved as C++:
NPP_SAVE cd $(CURRENT_DIRECTORY) g++ $(FILE_NAME) cmd /c $(CURRENT_DIRECTORY)\program.exe
Anyways whenever compiling a program, for example a simple program
#include <iostream>
using namespace std;
int main(){
cout << "Online\n";
system("pause"); //So that cmd doesn't disappear immeadiately on running.
return 0;
}
The console displays the following warning:
"C:\Users\pc\Desktop\Courses\Projects\C\program.exe' is not recognized as an internal or external command, operable program or batch file."
My question is, When I run the program on cmd, it runs perfectly but the error displayed during linking says that the folder does not exist in %PATH%
Any explanation?
Thank you!
Ok so, what I basically did was change the script,
cmd /c $(CURRENT_DIRECTORY)\program.exe
To be later
cmd /c $(CURRENT_DIRECTORY)\a.exe
the console worked fine and even received input
Here is a link to a similar problem:
How to compile/execute C++ code from within Notepad++

Can't execute compiled C++ exe file

I am having trouble executing my C++ code. I have written a basic "Hello World" program, and compiled it using the g++ make command. Here is my code:
#include <iostream>
using namespace std;
int main() {
cout << "Hello World" << endl;
return 0;
}
I am on Windows 10, using Emacs for code editing, and CygWin for compilation. I saved this file as hello.cpp. I then navigated to the directory in CygWin. Then I did the command make hello. This created hello.exe. Then, I attempted to execute the file using ./hello.exe. I also tried ./hello which also didn't work. When I type one of these commands and hit Enter, it just on the next line, not doing anything. I can type in this blank line, but it won't do anything. Does anyone know a way to make my code execute properly. Thank you.
EDIT: I tried running this at cpp.sh, an online C++ compiler, and it worked fine.
Your program probably is working but the console window is closing before you can see anything.
Try adding an input at the end of the program so it will wait.
I.E.
int a;
cin >> a;
Your code is most likely executing, but not outputting anything. That's because it's failing. Try checking the return value after it has run with echo $?. If it's not 0 then it has crashed. Also run it in gdb and see if it fails. The reason why it's failing is most likely a windows/cygwin clash - it's not your code.

Hello World C++

Every time I try and run the Simple Hello World program in C++, I build it and it says nothing to build for FirstProject. The code looks correct, I'm using MinGW as my compiler, etc. Every time I try to run the program, instead of printing the output, it just terminates. Anyone have a clue?
#include <iostream>
using namespace std;
int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
return 0;
}
try these commands:
g++ -o hello_world.exe hello_world.cpp
./hello_world.exe
MinGW works same as gcc on linux so all commands which work on linux should work on MinGW
In cygwin, the following steps should work.
Save the contents to file HelloWorld.cc.
Go to the directory where you saved the file.
Execute make HelloWorld
Execute ./HelloWorld.exe
If that doesn't work, something is really not right.
Are you running it from the command line or an IDE? Sometimes an IDE will open a terminal and close it too fast for you to see, or you'll just see a flash. Try running it from the command line so you'll be able to see if it printed anything before exiting the program.
Tried and tested using MinGW in windows 10 command prompt.
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello, World!"<<endl;
system("pause");
return 0;
}
Image of command prompt output
Save the contents to file HelloWorld.cpp
Go to the directory where you saved the file.
Execute make HelloWorld
Execute ./HelloWorld.exe

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

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 < > ... )

ofstream outputs in debug but not run

I have a very simple C++ program with a very simple project setup, but when I run the program I get no output. If I run the program in debug mode, it works perfectly. I am using Eclipse Kepler CDT 32 bit on windows with MinGW. I am somewhat new to eclipse, so it's probably something I did wrong.
The program is:
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
ofstream outfile("testdata.txt");
int main()
{
outfile << "Program Start\n";
cout << "Program Start\n";
return 0;
}
Help!
If the problem is that the program quickly opens and then closes before you can see the output on the screen, then you can just run your program from any shell (CMD on Windows, bash on Linux, etc.). That way, it won't exit once your program ends and you can see the results.
Make sure also that you flush/close your ofstream before your program exits.
The problem is rather not releted to c++ itself. You should check if via "cmd" typying it in "launch menu" after you click start. Find the path of your program, then run it.
For the very beginning it is recommended to spend a few hours with terminal(cmd). To know how things works. After that you will be independent - you will be able to write the code in any IDE. Also simple trick to make it working is to use std::cin.get() . It is prefered to system("pause").
You open the testdata.txt file using the relative path.
And the created file may be created in the project binary output path, where the executable located in.
You can use everything software to check whether a file is created and its created path.
everything
For example, you can type your output file name testdata.txt into everything software to see where output file created.And check if the testdata.txt created in a wrong path or directory.