C++ system() return values

Problem getting system() to return values

Page 1 of 1

10 Replies - 8153 Views - Last Post: 18 December 2009 - 12:29 PM Rate Topic: -----

#1 Logick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-December 09

C++ system() return values

Posted 18 December 2009 - 11:01 AM

Hi,

I'm having some problems with getting the system() command to return values in a program I'm compiling and running on Debian.

My code is fairly long at this stage, so an extract from my code that shows the relevant problem is:

string fileloc("./wcstools-3.8.1/bin/sky2xy");
string fileargx("-o x");
string fileargy("-o y");
string space(" ");

commandx = (fileloc + space + fileargx + space + file + space + rastr + space + decstr + space + coordsys).c_str();
commandy = (fileloc + space + fileargy + space + file + space + rastr + space + decstr + space + coordsys).c_str();

x = system(commandx);
y = system(commandy);

xyout << x << "\t\t" << y << endl;


where "rastr" and "decstr" are strings containing angular information in the format: ra(hh:mm:ss) & dec(dd:mm:ss), "file" is a pointer to a FITS image file (a format used in astrophysics) and "coordsys" is a coordinate system reference ("J2000" in this case).

The program compiles and runs well enough, but instead of returning a value to x and y the system() command just outputs the results to my terminal. It activates the sky2xy program (a coordinate system converting tool) and passes it the correct arguments (although it does not, as yet, give me the correct x and y values), but returns only 0s to x and y.

Should this command return a value to my program? If not, what else would you recommend to use?

A second problem is that when it does return a value, it will be an integer, not a double, for example causing 6.9 to be returned as 6. Is there any function that will execute a command in the terminal and return a double, or is there any other way around this integer problem?

Thanks for your help,
Ollie

Is This A Good Question/Topic? 0
  • +

Replies To: C++ system() return values

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,421
  • Joined: 23-August 08

Re: C++ system() return values

Posted 18 December 2009 - 11:10 AM

From man system:

Quote

Return Value


The value returned is -1 on error (e.g. fork() failed), and the return status of the command otherwise. This latter return status is in the format specified in wait(2). Thus, the exit code of the command will be WEXITSTATUS(status). In case /bin/sh could not be executed, the exit status will be that of a command that does exit(127).
If the value of command is NULL, system() returns non-zero if the shell is available, and zero if not.

system() does not affect the wait status of any other children


EDIT: You probably want to look into popen instead, if you need the text returned by the function.
Was This Post Helpful? 0
  • +
  • -

#3 Logick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-December 09

Re: C++ system() return values

Posted 18 December 2009 - 11:13 AM

Hi JackOfAllTrades,

I actually read that when I was trying to solve this earlier, but to be honest I didn't really understand all of it! Could you please clarify what it actually means, in rookie-speak?

Thanks,
Ollie

EDIT:

I tried popen also, as follows:

char buff[50];
		stringstream command;
		fp = popen(commandx, "r");

		while (fp != NULL)
		{
			while (fgets(buff, sizeof buff, fp) != NULL)
			{
				command << buff;
			}
		}
		pclose(fp);
	
		x = command.str();

		fp = popen(commandy, "r");

		while (fp != NULL)
		{
			while (fgets(buff, sizeof buff, fp) != NULL)
			{
				command << buff;
			}
		}
		pclose(fp);

		y = command.str();


but it didn't compile. Probably because I had no idea what I was doing.

This post has been edited by Logick: 18 December 2009 - 11:14 AM

Was This Post Helpful? 0
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: C++ system() return values

Posted 18 December 2009 - 11:33 AM

using system() -- will return the exit code of the applicaitons called.

So for example if I have an application called 'MyCmdApp.exe" that ends with the line: "return 100" then the system call:

x = system("MyCmdApp.exe");

well return 100.

For example on windows you can run the following program:
#include <iostream>

using namespace std;

int main() {
	int a = system("choice");
	cout << "returned: " << a <<endl;
	return 0;
}


If you press Y then 1 is returned N gets you 2...
Was This Post Helpful? 0
  • +
  • -

#5 Logick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-December 09

Re: C++ system() return values

Posted 18 December 2009 - 11:36 AM

NickDMax: ah, I think I see what the return element of system() does, then - it isn't really helpful for my purposes! Thanks for saving me a load more time trying to fiddle about with that.

Do you have any advice on getting popen to work and return a value (ideally a double)?

Again, thanks!

Ollie
Was This Post Helpful? 0
  • +
  • -

#6 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: C++ system() return values

Posted 18 December 2009 - 11:41 AM

So I take it that the program you are calling will print out some value to stdout?

And you would like to grab that value?
Was This Post Helpful? 0
  • +
  • -

#7 Logick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-December 09

Re: C++ system() return values

Posted 18 December 2009 - 11:45 AM

Correct, the value it prints out is a double representing a coordinate position.
Was This Post Helpful? 0
  • +
  • -

#8 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: C++ system() return values

Posted 18 December 2009 - 11:57 AM

So as I understand it, you use _popen/popen to open up a FILE object and then you can use fscanf to read off the double....
Was This Post Helpful? 0
  • +
  • -

#9 Logick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-December 09

Re: C++ system() return values

Posted 18 December 2009 - 12:05 PM

OK, so I now have

fp = popen(commandy, "r+");
float f;
fscanf(fp, %f, &f);
pclose(fp);


and I get the compile error "expected primary expression before '%' token". Any tips?

Thanks again,
Ollie
Was This Post Helpful? 0
  • +
  • -

#10 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: C++ system() return values

Posted 18 December 2009 - 12:20 PM

I think you need ot change you second parameter to "%f"
Was This Post Helpful? 1
  • +
  • -

#11 Logick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-December 09

Re: C++ system() return values

Posted 18 December 2009 - 12:29 PM

Thanks NickDMax, you've been extremely helpful and patient. Now all I need to do is fix my degrees -> hh:mm:ss conversion and my code should finally do what it's meant to do! yay, maths time.

Thanks again,
Ollie
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1