🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.sys.next.programmer
2 messages
2 total messages Started by Joseph A. Hollowood Sun, 30 Aug 1992 21:06
Can I pass variables to system()?
#915
Author: Joseph A. Hollowood
Date: Sun, 30 Aug 1992 21:06
15 lines
889 bytes
How can I make the following work?

The user types in a month (int) and a year (int) and I
pass those values to system("cal [month] [year]").

Here's the code I was experimenting with, but it wasn't
working...

   int theMonth = [inputFields intValueAt:0];
   int theYear = [inputFields intValueAt:1]; 
   system("cal $theMonth $theYear > /tmp/calOutputFile"); 

Please email any replies to me personally.

Joe
Re: Can I pass variables to system()?
#964
Author: Nick Kline
Date: Mon, 31 Aug 1992 23:44
30 lines
1532 bytes
In article <secOX9200WB2EMBEpC@andrew.cmu.edu> jh6l+@andrew.cmu.edu (Joseph A. Hollowood) writes:
>How can I make the following work?
>
>The user types in a month (int) and a year (int) and I
>pass those values to system("cal [month] [year]").
>
>Here's the code I was experimenting with, but it wasn't
>working...
>
>   int theMonth = [inputFields intValueAt:0];
>   int theYear = [inputFields intValueAt:1]; 
>   system("cal $theMonth $theYear > /tmp/calOutputFile"); 
>

No, the $variable thing only works in shell files, if that is what you
are suggesting.  You need to create a string that has what you want to
pass to the system call.  Use sprintf.  For your example:


   int theMonth = [inputFields intValueAt:0];
   int theYear = [inputFields intValueAt:1]; 
char string[1024];
   sprintf(string,"cal %d %d > /tmp/calOutputFile",theMonth,theYear);
   system(string);

string will contain what you want -- ie the values of the two variables.
sprintf works just like printf, except the output goes to a string.

-nick
kline@cs.arizona.edu
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads