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



On Thu, 21 Jun 2007 15:59:24 +0800,
Angelo Chen (email@hidden) wrote:


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

Ugh. This code is sloppy in so many ways.  Let me enumerate some...

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

I suggest you declare x as a const char *, so you'll
get a compiler error, rather than a crash, if you ever
attempt to modify the string.

>	FSSpec fspc;

You should really avoid using FSSpecs.  They have been
deprecated for a long time now.  You want to use FSRefs
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);

This will fail rather horribly if path is longer than
255 characters.  Prefer strncpy() over strcpy(), and
check string lengths.

>  return(FSMakeFSSpec(0, 0, (unsigned char *)buf,
>fss)); //== noErr

FSMakeFSSpec expects a colon-delimited HFS path, not a
slash-delimited POSIX path.

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

There is no guarantee that CFStringGetCStringPtr() will
return a pointer to a valid C string.  It may return NULL
even if you pass a valid CFString.  You want to use
CFStringGetCString() instead.  Also, the second parameter
to CFStringGetCStringPtr() is not a pointer -- it is a
string encoding -- you shouldn't ignore it.

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

The second parameter to FSpRename() should be the new file
name, not a path (either HFS or POSIX style).


                                    -- marco

-- 
It's not the data universe only, it's human conversation.
They want to turn it into a one-way flow that they have entirely
monetized. I look at the collective human mind as a kind of
ecosystem. They want to clear cut it. They want to go into the
rainforest of human thought and mow the thing down.

 _______________________________________________
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

References: 
 >Renaming a file (From: Angelo Chen <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.