Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Renaming a file



Am 21.06.2007 um 10:00 schrieb email@hidden:

Hi,

I have following code to rename a file, but I always
get error -43(file not found), any help? Thanks,

char *x = "/Users/justin/Movies/e1.mov";

Note: Hard-coded paths are ok for illustrating your problem here but please don't put them into production code.



	FSSpec fspc;
	path2fss(&fspc, x);
	err=FSpRename(&fspc,"/Users/justin/Movies/e1.movx");

Multiple problems here:

- Why are you using FSSpec instead of FSRef? FSSpec is going away and it can't handle modern Unicode Filenames without resorting to strange hacks.

- FSpRename expects a name not a path as the second argument. And also even if it would accept a path it would have to be HFS path not a POSIX path.

- FSpRename expects a pascal string as the second parameter, not a C string.


Use FSRenameUnicode() instead.


//path2fss makes an FSSpec from a path with or without
a filename
int path2fss(FSSpec *fss, char *path)
{
  char buf[256];
  char *p = &buf[1];
  strcpy(p, path); //convert to Str255	
  buf[0] = strlen(p);
  return(FSMakeFSSpec(0, 0, (unsigned char *)buf,
fss)); //== noErr
}

If you'd use FSRef then you could use the FSPathMakeRef() built-in function to create the FSRef from the path.



	char *x = "/Users/justin/Movies/e1.mov";		
	FSSpec fspc;
	path2fss(&fspc, CFStringGetCStringPtr(pathName,
nil));

- pathName is undefined!

- CFStringGetCStringPtr() does not guarante a non-NULL result. It's usually more hassle to wrap it safely than to just not use it all.

- The fspc result from this call is overwritten in the next line.


	path2fss(&fspc, x);
	err=FSpRename(&fspc,"/Users/justin/Movies/e1.movx");

See above.


To summarize:

Depending on how you actually get the path to the file in the first place you could do this:

UInt8 x[] = "/Users/justin/Movies/e1.mov";  // Just for illustration.
FSRef ref;
OSStatus err = FSPathMakeRef(x,&ref,NULL);

if (noErr == err)
{
// Just for illustration. Use a better method to get the new name in real code:
UniChar newName[] = {0x0065, 0x0031, 0x002e, 0x006d, 0x006f, 0x0076, 0x0078};


err = FSRenameUnicode(&ref,(UniCharCount)(sizeof(newName)/sizeof (UniChar)),newName,kTextEncodingUnknown,NULL);
}



HTH Mike -- Mike Fischer Softwareentwicklung, EDV-Beratung Schulung, Vertrieb Web: <http://homepage.mac.com/mike_fischer/index.html> Note: I read this list in digest mode! Send me a private copy for faster responses.

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Carbon-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/carbon-dev/email@hidden

This email sent to email@hidden


Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.