Ankata
Ankata

Reputation: 151

Return value of a command line program

I have an issue and I don't know my program is correct or not. Please let me know your ideas?

Issue: Create a process file program in command line and the return of program is the number of processed file.

My program: in main() function I return the number of processed file.

Is it correct? If correct, how can I get this value from another program?

Please help me?

Upvotes: 3

Views: 5290

Answers (3)

William Melani
William Melani

Reputation: 4268

You can simply use return. A common return value for Success is 0, and anything else is considered some sort of error.

int main()
{
 ...

return 0;
}

To get the value to another program, you can either use a System call, http://en.wikipedia.org/wiki/System_(C_standard_library)

or use a bash script like:

Edited, thanks Evan Teran:

  myProgram; 
    V=$?; 
    program1 $V

Upvotes: 5

Oswald
Oswald

Reputation: 31665

That's correct. The result code of the program is the return value of the main function.

Upvotes: 0

ngduc
ngduc

Reputation: 1413

main() can return "exit code" to OS by using exit(code) function

#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
    cout<<"Program will exit";
    exit(1); // Returns 1 to the operating system

    cout<<"This line is never executed";
}

Then in caller program, you can check returned exit code, for example (caller is a batch file):

@echo off
call yourapp.exe
echo Exit Code = %ERRORLEVEL%

Upvotes: 3

Related Questions