Create Random File Name?

Discussion in 'Mac Programming' started by Darkroom, Oct 20, 2008.

  1. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #1
    i don't really understand why this isn't working:

    Code:
    int randomNumber = (random() % 25000);
    NSString *pathName = (@"/tmp/picture%i.tiff", randomNumber);
    
    Warning: Initialization Makes Pointer From Integer Without Cast

    any ideas?
     
  2. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #2
    fixed

    asked too soon... as usual...

    Code:
    int randomNumber = (random() % 25000);
    NSString *pathName = [NSString stringWithFormat:@"/tmp/LightSwatch%i.tiff", randomNumber];
    
    although i'm still a touch hazy on the whole casting issue... i mean, i'm sorta/kinda know what it means, but it's not clear. i *think* i was receiving the first error because the program was trying to produce an NSString using an Int, but since they are different data types, the compiler issued the warning (and of course it didn't work)... that's what i think that warning meant anyway. and if i'm right, then i still don't understand why stringWithFormat worked... unless that's just the way to do it.
     
  3. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
  4. Krevnik macrumors 68040

    Krevnik

    Joined:
    Sep 8, 2003
    #4
    Another thing you can do if you need a temp file path is to use the function tempnam() to generate the C-string of the path.
     
  5. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #5
    Look at what you assign to pathName: You have two expressions separated by comma in parentheses; the first one is @"/tmp/picture%i/tiff", which is Objective-C for a constant NSString*, and the second is randomNumber, which is an int. Two expressions separated by comma are a "comma-expression": The compiler evaluates the first expression, then the second expression, and the result is the second expression. So what you did is the same as assigning NSString* pathName = randomNumber.

    Now I personally don't like the warning message; I would have written "attempt to assign an integer to a pointer", and it should most definitely be an error, not just a warning.

    The "without cast" is a red herring: Casting that expression to NSString* will get rid of the warning, but it won't fix the problem. It will take randomNumber, which is lets say 9999, cast it to an NSString* which means you will get a rubbish pointer, and when you try to use pathName you will most likely get a crash. So in this case adding a cast is the worst thing that you can do.
     
  6. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #6
    so is my fix not ok?

    Code:
    int randomNumber = (random() % 25000);
    NSString *pathName = [NSString stringWithFormat:@"/tmp/picture%i.tiff", randomNumber];
    it writes the files fine and seems to work... is there a better, less problematic way of producing this result?
     
  7. ChrisA macrumors G4

    Joined:
    Jan 5, 2006
    Location:
    Redondo Beach, California
    #7
    I have to second the above idea. Random filenames are a very bad idea. The above function will always give you a "good" filename that is not already in use. It will follow the file name rules of whatever system it is on.

    You ask "what are the chances of a random dom function giving two identical filenames?" It WILL happen if you program is used by enough people enough times
    AND, think hard about how to set the deed for the random functin or yu will get the same filenames every time you run the program.
     
  8. whooleytoo macrumors 603

    whooleytoo

    Joined:
    Aug 2, 2002
    Location:
    Cork, Ireland.
    #8
    When doing this, I always use the date & time (accurate to the millisecond), plus a sequence of unlikely characters. The date & time ensures that part of the name will be unique (at least until the clock overflows).

    And the sequence of chars is just in case someone else has the same bright idea and is putting files-named-using-date&time in the same folder. So, unless someone creates a file with the same unlikely sequence of characters in the same millisecond...

    Even then, I check if the file already exists and if it does, go again.
     
  9. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #9
    That's a fine fix, which contains no casts.
     

Share This Page