• 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
Writing NSString to a file descriptor
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Writing NSString to a file descriptor


  • Subject: Writing NSString to a file descriptor
  • From: AstroK Software <email@hidden>
  • Date: Tue, 20 Nov 2007 19:47:01 +0100

Hi,

I am writing a wrapper class around a library written by one of my colleagues, and I having troubles with writing an NSString to a file descriptor. The main function of the library takes a file descriptor as argument, and reads from that file descriptor. As my colleague cannot modify the library without changing a lot of things for a lot of people, I though I would create a NSFileHandle, get the associated file descriptor and pass it to the library function. Does it look like a good idea?

Just to test this possible solution I wrote the following test code:

- (void)useTool
{
NSString *msgStr = [[NSString alloc] initWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"test" ofType:@"spam"]];
NSData *msgData = [msgStr dataUsingEncoding:NSASCIIStringEncoding];
NSPipe *pipe = [NSPipe pipe];
NSFileHandle *fh = [pipe fileHandleForWriting];


	[[NSNotificationCenter defaultCenter] addObserver:self
						 selector:@selector(getData:)
						name:NSFileHandleReadCompletionNotification
						object:fh];
	[fh readInBackgroundAndNotify];
	[fh writeData:msgData];
}

- (void)getData:(NSNotification*)note
{
	NSFileHandle *fh = [note object];
	Tool *tool = [[Tool alloc] init];
	int res = 0;
	int fd = [fh fileDescriptor];

	res = [tool analyzeMessageFromFileDescriptor:fd];

	[spamc release];
	[fh release];
}

The problem is that it does not work... If I had:

NSLog(@"%d",fd);
NSLog(@"%@",[fh availableData]);

I get a file descriptor, but the second NSLog returns:

*** -[NSConcreteFileHandle availableData]: Bad file descriptor

Can someone help me? Maybe using a NSFileHandle is not the best solution... but if you know another one, I would be glad to here it!

Thanks a lot in advance,

 -- Arthur;

--
Arthur VIGAN
AstroK Software
email@hidden




_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Writing NSString to a file descriptor
      • From: Alastair Houghton <email@hidden>
  • Prev by Date: Re: Why is Leopard annoying my users?
  • Next by Date: Re: Why is Leopard annoying my users?
  • Previous by thread: Re: pdf importing
  • Next by thread: Re: Writing NSString to a file descriptor
  • Index(es):
    • Date
    • Thread