• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: command line tool output into a string?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: command line tool output into a string?


  • Subject: Re: command line tool output into a string?
  • From: Allan Odgaard <email@hidden>
  • Date: Sat, 24 Apr 2004 16:17:06 +0200

On 24. Apr 2004, at 12:13, Ben Dougall wrote:

Didn't catch the original message, thus replying to triple-quoted text!

i'm using a system() call to call a command line tool. the command
line tool's output is output to standard output. is there anyway to
get that output directly into a string rather than having to direct
it into a file, then open that file? if so, how?

I would suggest using popen(). It returns a FILE pointer from which you can read the output of the task.

Using something like CocoaSTL, that would then be (to get the output from "ls -l /"):
NSMutableString* str = [NSMutableString string];
if(FILE* fp = popen("/bin/ls -l /", "r"))
{
uint8_t buf[1024];
while(size_t len = fread(buf, 1, sizeof(buf), fp))
std::copy(buf, buf + len, back_inserter(str));
pclose(fp);
}

This assumes the output is in iso-8859-1 (latin 1).





** Cocoa FAQ: <http://www.alastairs-place.net/cocoa/faq.txt> **
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.


References: 
 >Re: command line tool output into a string? (From: Ben Dougall <email@hidden>)

  • Prev by Date: Re: Programmatically creating NSButton
  • Next by Date: Re: Programmatically creating NSButton
  • Previous by thread: Re: command line tool output into a string?
  • Next by thread: NSFileManager's error dictionary
  • Index(es):
    • Date
    • Thread