home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / sys / next / programm / 5900 < prev    next >
Encoding:
Internet Message Format  |  1992-08-31  |  1.5 KB

  1. Path: sparky!uunet!europa.asd.contel.com!darwin.sura.net!jvnc.net!yale.edu!yale!gumby!destroyer!ncar!noao!arizona!kline
  2. From: kline@cs.arizona.edu (Nick Kline)
  3. Newsgroups: comp.sys.next.programmer
  4. Subject: Re: Can I pass variables to system()?
  5. Message-ID: <21376@optima.cs.arizona.edu>
  6. Date: 31 Aug 92 23:44:04 GMT
  7. References: <secOX9200WB2EMBEpC@andrew.cmu.edu>
  8. Sender: news@cs.arizona.edu
  9. Organization: U of Arizona CS Dept, Tucson
  10. Lines: 30
  11.  
  12. In article <secOX9200WB2EMBEpC@andrew.cmu.edu> jh6l+@andrew.cmu.edu (Joseph A. Hollowood) writes:
  13. >How can I make the following work?
  14. >
  15. >The user types in a month (int) and a year (int) and I
  16. >pass those values to system("cal [month] [year]").
  17. >
  18. >Here's the code I was experimenting with, but it wasn't
  19. >working...
  20. >
  21. >   int theMonth = [inputFields intValueAt:0];
  22. >   int theYear = [inputFields intValueAt:1]; 
  23. >   system("cal $theMonth $theYear > /tmp/calOutputFile"); 
  24. >
  25.  
  26. No, the $variable thing only works in shell files, if that is what you
  27. are suggesting.  You need to create a string that has what you want to
  28. pass to the system call.  Use sprintf.  For your example:
  29.  
  30.  
  31.    int theMonth = [inputFields intValueAt:0];
  32.    int theYear = [inputFields intValueAt:1]; 
  33. char string[1024];
  34.    sprintf(string,"cal %d %d > /tmp/calOutputFile",theMonth,theYear);
  35.    system(string);
  36.  
  37. string will contain what you want -- ie the values of the two variables.
  38. sprintf works just like printf, except the output goes to a string.
  39.  
  40. -nick
  41. kline@cs.arizona.edu
  42.