Professional Web Applications Themes

File creating problem - Mac Programming

Hello, I'm trying to test if a file exists and if not I try to create it in order to open it for writing. So I first use FSpGetFInfo to check if the file exists, then if iErr==fnfErr I try to create the file. In my example below cpath="/Users/surfdargent/test" and is an absolute path. My first problem : apparently even if the file exists (I just create it with 'vi') FSpGetFInfo returns fnfErr and then my prog tries to create the file. But now FSpCreate return -37 (bdNamErr) which should mean something like bad filename or bad volume. Did I ...

  1. #1

    Default File creating problem

    Hello,

    I'm trying to test if a file exists and if not I try to create it in
    order to open it for writing.
    So I first use FSpGetFInfo to check if the file exists, then if
    iErr==fnfErr I try to create the file.
    In my example below cpath="/Users/surfdargent/test" and is an absolute
    path.
    My first problem : apparently even if the file exists (I just create
    it with 'vi') FSpGetFInfo returns fnfErr and then my prog tries to
    create the file.
    But now FSpCreate return -37 (bdNamErr) which should mean something
    like bad filename or bad volume.

    Did I do something wrong ?
    Another thing : I haven't found any doent explaining the creator
    and filetype stuff yet... could someone quickly explain what
    parameters I need to pass to FSpCreate for a jpeg file ?

    I have no experience in manipulating FS under MacOS X... any help is
    appreciated. Regards.

    Here is my piece of code :

    cerr<<"Trying to open file : "<<cpath<<endl;
    FSMakeFSSpec( 0, 0, cpath, &spec);
    short fp;
    long nbwritten;
    // Create file if doesn't exist
    if( (iErr = FSpGetFInfo( &spec, &finfo)) != noErr)
    {
    // If file was not found we create it
    if( iErr == fnfErr)
    {
    iErr = FSpCreate( &spec, 'JPEG', 'JPEG', smSystemScript);
    if( iErr)
    DoError( iErr, "FSpCreate failed.");
    }
    else
    DoError( iErr, "FSpGetFInfo failed.");
    }
    // Open for writing
    iErr = FSpOpenDF( &spec, fsWrPerm, &fp);
    if (iErr)
    DoError( iErr, "FSpOpenDF failed.");
    iErr = FSpOpenRF( &spec, fsWrPerm, &fp);
    if (iErr)
    DoError( iErr, "FSpOpenRF failed.");
    // Write the jpeg data
    iErr = FSWrite( fp, &nbwritten, jpeg_data);
    if (iErr)
    DoError( iErr, "FSWrite failed.");
    // Close the file
    FSClose( fp);
    Stéphane Vaxelaire Guest

  2. #2

    Default Re: File creating problem

    In article <bca37bb1.0307240435.179818eposting.google.com> ,
    [email]surfdargentfree.fr[/email] (Stéphane Vaxelaire) wrote:
    > I'm trying to test if a file exists and if not I try to create it in
    > order to open it for writing.
    > So I first use FSpGetFInfo to check if the file exists, then if
    > iErr==fnfErr I try to create the file.
    > In my example below cpath="/Users/surfdargent/test" and is an absolute
    > path.
    > My first problem : apparently even if the file exists (I just create
    > it with 'vi') FSpGetFInfo returns fnfErr and then my prog tries to
    > create the file.
    > But now FSpCreate return -37 (bdNamErr) which should mean something
    > like bad filename or bad volume.
    >
    > Did I do something wrong ?
    fnfErr will also be returned if you are incorrectly specifying the path
    or incorrectly converting it into an FSSpec.

    Similarlly, if this spec is incorrect, you'll get an "bad name error"
    when attempting to create it.

    Your path is a Unix-style path, which is invalid for calls into the
    Carbon file manager. I recommend MoreFiles.c and example code on
    Apple's web site for more information on how to handle absolute paths.
    Tom Dowdy Guest

  3. #3

    Default Re: File creating problem

    On 24 Jul 2003, Stéphane Vaxelaire wrote:
    > Hello,
    >
    > I'm trying to test if a file exists and if not I try to create it in
    > order to open it for writing.
    > So I first use FSpGetFInfo to check if the file exists, then if
    > iErr==fnfErr I try to create the file.
    > In my example below cpath="/Users/surfdargent/test" and is an absolute
    > path.
    > My first problem : apparently even if the file exists (I just create
    > it with 'vi') FSpGetFInfo returns fnfErr and then my prog tries to
    > create the file.
    Firstly, when you call FSMakeFSSpec, it will return noErr if the
    file/folder you are specifying exists, and fnfErr if all the elements of
    the path exist, but not the actual file.

    Secondly, the path separator used with HFS APIs is ':', not '/'

    Lastly using 0 for dirID and vRefNum does not mean start from the root of
    the disk, it means use the applications default directory, which is
    usually the folder the application is in (unless you've changed it), if
    your app is bundled i can't rmemember if you get the folder containing the
    bundle or the one containing the executable.

    You should probably be looking at the CFURLRef functions, or FSPathMakeRef
    (which makes FSRefs).

    Fred

    Frederick Cheung Guest

  4. #4

    Default Re: File creating problem

    In article
    <Pine.LNX.4.44.0307242134460.15938-100000kern.srcf.societies.cam.ac.uk>,
    Frederick Cheung <fglc2srcf.DUH.ucam.org> wrote:
    > Lastly using 0 for dirID and vRefNum does not mean start from the root of
    > the disk, it means use the applications default directory, which is
    > usually the folder the application is in (unless you've changed it), if
    > your app is bundled i can't rmemember if you get the folder containing the
    > bundle or the one containing the executable.
    If the path is absolute (i.e. starts with a non-':' and contains a ':') then it
    overrides any volref/dirid.

    meeroh
    Miro Jurisic Guest

Similar Threads

  1. problem in binding xml file data to datagrid xml file isgenerated through JSP file
    By ckshah in forum Macromedia Flex General Discussion
    Replies: 2
    Last Post: May 26th, 03:55 PM
  2. Problem creating PDF file using Acrobat PDFWriter PDFFileName
    By RichW in forum Adobe Acrobat Windows
    Replies: 0
    Last Post: May 10th, 02:38 PM
  3. Problem creating link to a file
    By karamatic@adobeforums.com in forum Adobe Acrobat Windows
    Replies: 0
    Last Post: May 6th, 02:40 PM
  4. Creating PDF file
    By PT_M.A._Rosenberg@adobeforums.com in forum Adobe Acrobat Windows
    Replies: 0
    Last Post: April 28th, 02:09 PM
  5. creating a new file
    By MJS in forum PERL Miscellaneous
    Replies: 5
    Last Post: August 12th, 05:13 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139