Article View: comp.sys.next.programmer
Article #964Re: Can I pass variables to system()?
From: Nick Kline
Date: Mon, 31 Aug 1992 23:44
Date: Mon, 31 Aug 1992 23:44
30 lines
1532 bytes
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
Technical Details
Message-ID:
<21376@optima.cs.arizona.edu>Path:
arc1-ca.pugleaf.net!sparky!uunet!europa.asd.contel.com!darwin.sura.net!jvnc.net!yale.edu!yale!gumby!destroyer!ncar!noao!arizona!klineReferences:
<secOX9200WB2EMBEpC@andrew.cmu.edu>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!europa.asd.contel.com!darwin.sura.net!jvnc.net!yale.edu!yale!gumby!destroyer!ncar!noao!arizona!kline From: kline@cs.arizona.edu (Nick Kline) Newsgroups: comp.sys.next.programmer Subject: Re: Can I pass variables to system()? Message-ID: <21376@optima.cs.arizona.edu> Date: 31 Aug 92 23:44:04 GMT References: <secOX9200WB2EMBEpC@andrew.cmu.edu> Sender: news@cs.arizona.edu Organization: U of Arizona CS Dept, Tucson Lines: 30