Mac file copy function

Posted on 2004-11-17
Last Modified: 2013-12-26

Urgently needed Mac File Copy function with only two parameters: source and destination POSIX-pathname.

OSErr MacSimpleFileCopy( const char * srcName, const char * dstName );

OR two CFURLRef parameters:

OSErr MacSimpleFileCopy( CFURLRef srcName, CFURLRef dstName );

Required: Mac OS X/Carbon, copying data & resource forks
Unneeded: network support, Mac OS Classic support

I'm newbie in mac world, and completely confused with FSSpec, FSRef, dirID etc.
Question by:Korsar_13
    LVL 2

    Expert Comment

    This is very simple copy file function that you can use in Mac (or Windows):

    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>

    int copyFile (const char *inPath, const char * outPath) {
      int inF, ouF;
      char line[512];
      int bytes;

      if((inF = open( inPath , O_RDONLY)) == -1) {

      if((ouF = open(outPath , O_WRONLY | O_CREAT)) == -1) {

      while((bytes = read(inF, line, sizeof(line))) > 0)
        write(ouF, line, bytes);


    LVL 1

    Author Comment

    unfortunately, this routine don't copy FileType/FileCreator tags and resource fork suggest me use FSCopyOject, but it's parameters is very strange to me:

    /* This routine acts as the top level of the copy engine.  */
    OSErr FSCopyObject(  const FSRef        *source,
              const FSRef        *destDir,
               ItemCount        maxLevels,
               FSCatalogInfoBitmap    whichInfo,
               DupeAction        dupeAction,
               const HFSUniStr255    *newObjectName,  /* can be NULL */
              Boolean          wantFSSpec,
              Boolean          wantName,
              CopyObjectFilterProcPtr filterProcPtr,  /* can be NULL */
              void          *yourDataPtr,  /* can be NULL */
              FSRef          *newObjectRef,  /* can be NULL */
              FSSpec          *newObjectSpec)  /* can be NULL */

    LVL 4

    Accepted Solution

    Well, there should be only two tricky bits, if you're doing this for a simple file. First off, you need to change your source path into an FSRef. Do that like this.

       FSRef srcRef;
       if (!CFURLGetFSRef (srcURLPath, &srcRef))
         return bdNamErr;

    Next, you need an FSRef to the directory you're copying to; get that the same way.

    If you're not changing the name, then you're done, call FSCopyObject with those two FSRefs and everything else NULL.

    If you are changing the name, then you need one more parameter, the HFSUniStr255 of the new name you want. Put the new name into a CFStringRef from whatever source format you have it in, then use this to get an HFSUniStr255 out of that.

    HFSUniStr255 name;
    name.length = CFStringGetBytes( input, CFRangeMake(0, MIN(CFStringGetLength(input), 255)),
            kCFStringEncodingUnicode, 0, false, (UInt8 *)(name.unicode), 255, NULL );
    if ( output->length == 0 )
        ;// handle error

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Suggested Solutions

    Title # Comments Views Activity
    Unix / Linux grid computing 5 96
    How to determine file rename from EventLog ? 2 33
    matchUp  challenge 6 35
    Problem to event 3 26
    Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
    Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now