File creating problem

Ask a Question related to Mac Programming, Design and Development.

  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 document 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. Similar Questions and Discussions

    1. problem in binding xml file data to datagrid xml file isgenerated through JSP file
      problem it that i am creating xml file using JSP file and i want to bind DataGrid with xml file data that is created using JSP but it will not Bind...
    2. Problem creating PDF file using Acrobat PDFWriter PDFFileName
      Good morning. I'm having a problem that a think may be related to Acrobat 6.0 as I had this working with 5.0. Bascially, I am setting the...
    3. Problem creating link to a file
      Hi, I'm using Acrobat Professional 6.0 and have the need to often editing PDFs which are on network shares, creating links to other files which are...
    4. Creating PDF file
      I am new to Adobe usage and am trying to create a PDF file that will need to be created from word documents as well as a powerpoint program. This...
    5. creating a new file
      I need to write a script which when run will create a new text file depending if there is another text file which contains certain string e.g. ....
  3. #2

    Default Re: File creating problem

    In article <bca37bb1.0307240435.179818e@posting.google.com> ,
    [email]surfdargent@free.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

  4. #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

  5. #4

    Default Re: File creating problem

    In article
    <Pine.LNX.4.44.0307242134460.15938-100000@kern.srcf.societies.cam.ac.uk>,
    Frederick Cheung <fglc2@srcf.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

Posting Permissions

  • You may not post new threads
  • You may 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