• 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
Issue with read/write to NSFileHandle
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Issue with read/write to NSFileHandle


  • Subject: Issue with read/write to NSFileHandle
  • From: Graham Cox <email@hidden>
  • Date: Thu, 30 Jun 2016 13:40:55 +1000

Hi all,

I’m not sure whether what I’m doing is right here. I’m trying to implement a simple logging system, where I write to a log file, and a NSTextView shows what’s in the log.

I have a NSFileHandle that I create using +[NSFileHande fileHandleForUpdatingAtURL:error:]

I write new log messages to it using -writeData:, and that works - I can see that the file itself has been updated with the new information.

In order to read the content of the file, I have set up a readability handler like this:

				mLogFile.readabilityHandler = ^(NSFileHandle* handle)
				{
					NSData* available = [handle availableData];
					NSString* newText = [[NSString alloc] initWithData:available encoding:NSUTF8StringEncoding];
					[self performSelectorOnMainThread:@selector(appendLogText:) withObject:newText waitUntilDone:NO];
					[newText release];
				};


I do this rather than directly updating the log text view when I write the data so that in theory I could have other stuff writing stuff to the same file. This block does get called when the file is updated, but -availableData is always zero bytes. Reading the documentation, NSFileHandle seems to have only one file offset, which is common to both read and write, which is a bit puzzling, because surely you’d want to write at the end of the file, but read from wherever you’d read from last time. This single offset explains why I always get zero bytes, but it seems like a strange implementation. Have I understood it correctly? Can I achieve what I want, or do I have to save off the previous file offset? If I do that, I may as well write the text directly when I update the file. This isn’t really what I want, though could probably live with it for now.

—Graham



_______________________________________________

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: Issue with read/write to NSFileHandle
      • From: Jens Alfke <email@hidden>
    • Re: Issue with read/write to NSFileHandle
      • From: Shane Stanley <email@hidden>
  • Prev by Date: Re: Issue with read/write to NSFileHandle
  • Next by Date: Re: Issue with read/write to NSFileHandle
  • Previous by thread: Re: Using va_list to build a formatted string
  • Next by thread: Re: Issue with read/write to NSFileHandle
  • Index(es):
    • Date
    • Thread