[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Redirecting standard output to a file
From: |
address@hidden |
Subject: |
Re: Redirecting standard output to a file |
Date: |
Sun, 29 Jun 2008 09:13:36 -0700 (PDT) |
User-agent: |
G2/1.0 |
On 29 Jun., 13:15, Michael Hopkins <michael.hopk...@-rm-this-hopkins-
research.com> wrote:
> Hi all
>
> I have a question about using the classes in base to redirect standard
> output to a file without too much hassle or re-writing of code. I have
> classes that print themselves to standard output like this:
>
> [ someObject show ];
>
> ..but would like to be able to redirect this behaviour within code and
> possibly in response to user input. Something like...
>
> [ outputFile appendedWith:[ someObject show ]];
>
> I suspect that NSPipe and/or NSFileHandle might be involved but welcome
> any tips.
>
> TIA
>
> Michael
>
> --
> ______________________________________________
>
> Hopkins Research Touch the Future
> ______________________________________________
Some de-facto standard is that objects derived from NSObject should
respond to - (NSString *) description which returns a user-readable
description.
Then, you can write
[[NSFileHandle fileHandleWithStandardOutput] writeData:[[object
description] dataUsingEncoding:NSUTF8StringEncoding]];
If you prefer to mix OpenSTEP and POSIX:
printf("%s\n", [[object description] UTF8String]);
You could also pack that into a Macro (similar to NSLog which first
builds a formatted string and then writes to StandardErrlr).
What you also could do in main():
freopen(stdout, "path to file", "w");
-- hns