Trouble using createFileAtPath method to create file

  • Hi,
    I'm having trouble using the createFileAtPath method to create a file in the
    following directory:
    /private/var/tmp/folders.501/Cleanup At Startup

    The code that I am using to attempt creating the file is:
        NSString* pFP = [(NSString*)sourcePath stringByAppendingPathComponent:
                            [NSString stringWithCString:fileName encoding:30]];
        BOOL bRes = NO;

        if ( (bRes=[[NSFileManager defaultManager] createFileAtPath:pFP
    contents:nil attributes:nil]) == YES)
            return true;
        else
            return false;

    The code cannot create the file in the private folder, but if I change the
    path to the home directory, I am able to create the file. What has to be
    done in order to create a file in the "/private/var/tmp/folders.501/Cleanup
    At Startup" directory? Thanks in advance!

    --
    -Jaime
    http://www.jaimerios.com
  • On Oct 12, 2007, at 9:10 AM, Jaime Rios wrote:

    > Hi,
    > I'm having trouble using the createFileAtPath method to create a
    > file in the
    > following directory:
    > /private/var/tmp/folders.501/Cleanup At Startup
    >
    > The code that I am using to attempt creating the file is:
    > NSString* pFP = [(NSString*)sourcePath
    > stringByAppendingPathComponent:
    > [NSString stringWithCString:fileName
    > encoding:30]];

    1) What is encoding 30?  You should use one of the predefined
    symbolic constants, not some value like 30.

    2) How are you getting that sourcePath?  If you've hardcoded it in,
    you're just begging for trouble, even if it it does work (since that
    won't work for other users).  Your best bet is to use FSFindFolder,
    use CFURLCreateFromFSRef to convert that fsref to a URL, and then get
    the path from that.

    Glenn Andreas                      <gandreas...>
      <http://www.gandreas.com/> wicked fun!
    quadrium2 | build, mutate, evolve, animate  | images, textures,
    fractals, art
  • Actually, that is the older code where I was using a const char* for the
    file name. I'm not using that anymore and instead I am using a CFStringRef
    for the file name and a CFURLRef for the file path. However, it still fails
    to create a file at that path:

    NSString* pFP = [(NSString*)CFURLCopyPath(sourcePath)
    stringByAppendingPathComponent:
            (NSString*)fileName];

    Any ideas? Thanks!

    --
    -Jaime
    http://www.jaimerios.com

    On 10/12/07, glenn andreas <gandreas...> wrote:
    >
    >
    > On Oct 12, 2007, at 9:10 AM, Jaime Rios wrote:
    >
    >> Hi,
    >> I'm having trouble using the createFileAtPath method to create a
    >> file in the
    >> following directory:
    >> /private/var/tmp/folders.501/Cleanup At Startup
    >>
    >> The code that I am using to attempt creating the file is:
    >> NSString* pFP = [(NSString*)sourcePath
    >> stringByAppendingPathComponent:
    >> [NSString stringWithCString:fileName
    >> encoding:30]];
    >
    > 1) What is encoding 30?  You should use one of the predefined
    > symbolic constants, not some value like 30.
    >
    > 2) How are you getting that sourcePath?  If you've hardcoded it in,
    > you're just begging for trouble, even if it it does work (since that
    > won't work for other users).  Your best bet is to use FSFindFolder,
    > use CFURLCreateFromFSRef to convert that fsref to a URL, and then get
    > the path from that.
    >
    >
    >
    >
    >
    > Glenn Andreas                      <gandreas...>
    > <http://www.gandreas.com/> wicked fun!
    > quadrium2 | build, mutate, evolve, animate  | images, textures,
    > fractals, art
    >
    >
    >
  • On Oct 12, 2007, at 9:27 AM, Jaime Rios wrote:

    > Actually, that is the older code where I was using a const char*
    > for the
    > file name. I'm not using that anymore and instead I am using a
    > CFStringRef
    > for the file name and a CFURLRef for the file path. However, it
    > still fails
    > to create a file at that path:
    >
    > NSString* pFP = [(NSString*)CFURLCopyPath(sourcePath)
    > stringByAppendingPathComponent:
    > (NSString*)fileName];
    >
    > Any ideas? Thanks!

    So what value is pFP?  Is it actually a valid full file path?  I use
    a similar technique (get the "chewable" folder via FindFolder, and
    eventually append file names to that) for temporary files with no
    problems...

    Glenn Andreas                      <gandreas...>
      <http://www.gandreas.com/> wicked fun!
    quadrium | flame : flame fractals & strange attractors : build,
    mutate, evolve, animate
  • Hi,
    The value pFP is :
    /private/var/tmp/folders.501/Cleanup At Startup/123456

    where 123456 is the filename.

    --
    -Jaime
    http://www.jaimerios.com

    On 10/12/07, glenn andreas <gandreas...> wrote:
    >
    >
    > On Oct 12, 2007, at 9:27 AM, Jaime Rios wrote:
    >
    >> Actually, that is the older code where I was using a const char*
    >> for the
    >> file name. I'm not using that anymore and instead I am using a
    >> CFStringRef
    >> for the file name and a CFURLRef for the file path. However, it
    >> still fails
    >> to create a file at that path:
    >>
    >> NSString* pFP = [(NSString*)CFURLCopyPath(sourcePath)
    >> stringByAppendingPathComponent:
    >> (NSString*)fileName];
    >>
    >> Any ideas? Thanks!
    >
    > So what value is pFP?  Is it actually a valid full file path?  I use
    > a similar technique (get the "chewable" folder via FindFolder, and
    > eventually append file names to that) for temporary files with no
    > problems...
    >
    > Glenn Andreas                      <gandreas...>
    > <http://www.gandreas.com/> wicked fun!
    > quadrium | flame : flame fractals & strange attractors : build,
    > mutate, evolve, animate
    >
    >
    >
    >
  • Yes, the "Cleanup At Startup" folder exists prior to me trying to create the
    file.

    --
    -Jaime
    http://www.jaimerios.com

    On 10/12/07, Emanuele $B!g(B Vulcano <me...> wrote:
    >
    >
    > Il giorno 12/ott/07, alle ore 16:27, Jaime Rios ha scritto:
    >
    >> NSString* pFP = [(NSString*)CFURLCopyPath(sourcePath)
    >> stringByAppendingPathComponent:
    >> (NSString*)fileName];
    >
    > This is bad because it will leak a string. Core Foundation does not
    > follow the same rules as Cocoa; in particular *Copy* functions work
    > like Cocoa's init methods.
    >
    > More correct:
    >
    > NSString* pFPPrefix = [(NSString*) CFURLCopyPath(sourcePath)
    > autorelease];
    > NSString* pFP = [pFPPrefix stringByAppending...];
    >
    > Back to your original problem: does the folder whose path is pFP
    > actually *exist* before you try to create a file in it?
    >
    > - $B!g(B
    >
    >
  • Oh, and thanks for the memory leak tip!

    --
    -Jaime
    http://www.jaimerios.com

    On 10/15/07, Jaime Rios <xdev74...> wrote:
    >
    > Yes, the "Cleanup At Startup" folder exists prior to me trying to create
    > the file.
    >
    >
    > --
    > -Jaime
    > http://www.jaimerios.com
    >
    > On 10/12/07, Emanuele $B!g(B Vulcano <me...> wrote:
    >>
    >>
    >> Il giorno 12/ott/07, alle ore 16:27, Jaime Rios ha scritto:
    >>
    >>> NSString* pFP = [(NSString*)CFURLCopyPath(sourcePath)
    >>> stringByAppendingPathComponent:
    >>> (NSString*)fileName];
    >>
    >> This is bad because it will leak a string. Core Foundation does not
    >> follow the same rules as Cocoa; in particular *Copy* functions work
    >> like Cocoa's init methods.
    >>
    >> More correct:
    >>
    >> NSString* pFPPrefix = [(NSString*) CFURLCopyPath(sourcePath)
    >> autorelease];
    >> NSString* pFP = [pFPPrefix stringByAppending...];
    >>
    >> Back to your original problem: does the folder whose path is pFP
    >> actually *exist* before you try to create a file in it?
    >>
    >> - $B!g(B
    >>
    >>
    >
    >
previous month october 2007 next month
MTWTFSS
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        
Go to today