discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Redirecting standard output to a file


From: Michael Hopkins
Subject: Re: Redirecting standard output to a file
Date: Fri, 11 Jul 2008 00:55:18 +0100
User-agent: Unison/1.8.1

On 2008-06-29 17:13:36 +0100, "hns@computer.org" <hns@computer.org> said:

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

Thanks for the ideas. Will think about these.

The 'show' method is implemented directly as printf() or uses C functions in a separate library that use printf(), so that will not be straightforward easy to achieve unless I have misunderstood you, which is quite possible as I'm relatively new to the OpenStep API.

Was hoping to use Cocoa/GNUstep goodness to make it a simple task. Any other ideas short of re-implementing the functions to use fprintf() and parameterising the file to send to? Would overriding the 'description' method to call [ self show ] achieve this result?

TIA

M

--
______________________________________________

   Hopkins Research      Touch the Future
______________________________________________



reply via email to

[Prev in Thread] Current Thread [Next in Thread]