There is a script that I have been tasked to run and modify. However, I am having quite difficulty in understanding the signifigance of return codes, and it would make my life easier if I can redirect the system console output to file.

In Windows, I tried going to run, typing in "cmd" to run the following command:

C:\[name of scipt.exe] >> filetooutput.txt

But the program just hangs.

Here is a snippit of code in the script:

result=system(outputx); 

outFile << "For  IP address" << abc << " We have attempted to install xyz and the result is " << result << endl;
outFile.close();

outputx is carefully assembled string to be sent to system console.

Once again, looking forward to your guidance.

Recommended Answers

All 4 Replies

Why do you want to save the output to a file just to understand return codes? The return code from system() is pretty much useless -- all it indicates is whether system() successfully ran the command or not. It says nothing about the program that system() ran. If you need to return code of the program (not system()) then you need to spawn it in some other manner, such as on MS-Windows use CreateProcess(), WaitForSingleObject(), then GetExitCodeProcess(). Here is a link with sample code.

The return code from system() is pretty much useless -- all it indicates is whether system() successfully ran the command or not. It says nothing about the program that system() ran.

That's not quite so. Basically system() returns the value from the command interpreter i.e. it can well be used like the OP is planning to use it. MSDN documentation about system()

That's not quite so. Basically system() returns the value from the command interpreter i.e. it can well be used like the OP is planning to use it. MSDN documentation about system()

Like I said -- the return value of the command interpreter is also pretty much useless. The command interpreter does not return the exit status (or exit value) of the program/process that was executed. If your program doesn't need that, then no harm done. Otherwise don't get the false impression that system() returns something that it does not.

Like I said -- the return value of the command interpreter is also pretty much useless. The command interpreter does not return the exit status (or exit value) of the program/process that was executed. If your program doesn't need that, then no harm done. Otherwise don't get the false impression that system() returns something that it does not.

I refuse to agree with that. Say you have the following two programs ..

// 42.cpp
// this one produces a program named 42.exe

int main(int argc, char* argv[])
{
	// simply return 42
	return 42;
}
// test.cpp
// this one produces a program named test.exe

#include <iostream>

int main(int argc, char* argv[])
{
	int result = system("42.exe");
	std::cout << "-> " << result << std::endl;

	return 0;
}

Assuming 42.exe is in the same directory as test.exe, the output of test.exe is:
-> 42

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.