• 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: creating files to write data to?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: creating files to write data to?


  • Subject: Re: creating files to write data to?
  • From: Ken Thomases <email@hidden>
  • Date: Sun, 17 Aug 2008 00:52:01 -0500

On Aug 17, 2008, at 12:07 AM, FTB Accounts wrote:

Here is the current code I am running:

This code has many fundamental errors. I think you need to review the basics of the C language first, then of Objective-C. You have to walk before you can run.


You say that you're not getting any errors or warnings when you build this, but that can't be. You would definitely get warnings for this code.

/* START CODE */

#import <Cocoa/Cocoa.h>

int main(int argc, char *argv[])
{

NSData *fname = "file:///Users/cknorr/mytest/MYTEST/data.txt";

You are initializing a variable of type NSData* (pointer to NSData). However, the right-hand side is not an NSData*, it's a C-style string, which is a character array (char* or char[]). These are not compatible. It's certainly not meaningful. Any attempt to use the fname variable as though it were a pointer to an NSData object would fail, because it doesn't actually contain a valid pointer to an NSData object.


NSFileHandle *fh=[NSFileHandle fileHandleForWritingAtPath:fname];

Even if fname did contain a valid pointer to an NSData object, the above would not be correct. The +fileHandleForWritingAtPath: method of NSFileHandle expects a pointer to an NSString object as its argument, but that's not what you're providing.


Note that a pointer to a C-style string (character array), which is what's in fname, is also _not_ a valid pointer to an NSString object. They are two completely different ways of representing strings and are not directly interchangeable.

[fh writeData:@"THIS IS A TEST"];

The -writeData: method of NSFileHandle expects a pointer to an NSData object. However, what you're passing here is a pointer to an NSString object. Again, this can't work.



[fh closeFile]; return NSApplicationMain(argc, (const char **) argv); }

Regards, Ken

_______________________________________________

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: creating files to write data to?
      • From: Scott Anguish <email@hidden>
References: 
 >Re: creating files to write data to? (From: FTB Accounts <email@hidden>)

  • Prev by Date: Re: creating files to write data to?
  • Next by Date: Re: creating files to write data to?
  • Previous by thread: Re: creating files to write data to?
  • Next by thread: Re: creating files to write data to?
  • Index(es):
    • Date
    • Thread