How Can I Read a File With a Relative Path Name

Discussion in 'Mac Programming' started by kaydell.leavitt, Apr 30, 2010.

  1. kaydell.leavitt macrumors regular

    Joined:
    Apr 19, 2010
    #1
    I'm trying to read a file with a relative pathname, but it's not working.

    Can somebody help me out?

    Thanks.

    Code:
    - (void) drawRect:(NSRect)dirtyRect {
    	NSString *directory = @".";
    	directory = [directory stringByStandardizingPath];
    	NSString *fileName = @"/Logo.png";
    	NSString *string = [NSString stringWithFormat:@"%@%@%@", @"file://localhost/", directory, fileName];
    	NSURL *url = [[NSURL alloc] initWithString:string];
    	NSData *data = [NSData dataWithContentsOfURL:url]; <<< data is nil <<<
    
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    Display the resulting string, before you make it into a URL. Confirm the file actually exists at that location. Post the result.

    Since you're referring to a file on localhost, you can avoid the URL entirely and use one of the NSData methods that returns the contents of a file given by a pathname string.

    There are also NSData methods that return an NSError by reference. You might use one of those, and use the NSError or its sub-components to give you more detail on exactly what the problem is.
     
  3. kaydell.leavitt thread starter macrumors regular

    Joined:
    Apr 19, 2010
    #3
    Thanks, That Works Better

    Thanks, that works better. Here is my new code:

    Code:
    - (void) drawRect:(NSRect)dirtyRect {
    	NSString *path = @"./Preferences/Logo.png";
    	NSData *data = [NSData dataWithContentsOfFile:path];
    	NSImage *image = [[NSImage alloc] initWithData:data];
    	[self setImage:image];
    	[super drawRect:dirtyRect];
    }
    
    @end
    
    The curious thing about the above code is that it works in debug mode, but not in release mode (I don't get it!).

    -- Kaydell :):confused:
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    In debug mode the cwd may be were you think it is. In release mode I'm pretty sure it isn't. Where are you trying to read from? Within the application? If so use NSBundle to find the path. That's the correct way to do it.
     
  5. kaydell.leavitt thread starter macrumors regular

    Joined:
    Apr 19, 2010
    #5
    No, I'm trying to read from the Preferences folder which is within my applications folder. The reason that I'm reading from my applications Preferences folder is because the user should be able to drop in their own logo which I don't know at compile-time, so I don't want to keep the logo within my application's bundle.
     
  6. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    Well, you're not putting that in the correct places: it should be in Application Support. Which you can get the path to programatically using the usual function.
     
  7. kaydell.leavitt thread starter macrumors regular

    Joined:
    Apr 19, 2010
    #7
    Thanks Everyone

    Thanks for all of your help.

    It's working now. I just had to put the logo in two places. Once in the Debug Folder and once again in the Release Folder.

    Thanks again.

    -- Kaydell :):):)
     
  8. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #8
    Please see my advice above: you should not rely on a folder in /Applications. Any well written application could be moved anywhere on the filesystem. Or run from a location that the user can't write to. Apple provide clear guidelines on this: use ~/Application Support/. A really well written app would look in the user, then system, then network domains (in Application Support) in that order.
     
  9. kaydell.leavitt thread starter macrumors regular

    Joined:
    Apr 19, 2010
    #9
    OK. Here is my code to read the logo from the application bundle. It perplexes me that the code works in debug but not for the release target.

    Code:
    	NSBundle *mainBundle = [NSBundle mainBundle];  // <<<<< works in debug mode, but returns nil in release mode
    
    :confused:
     
  10. Thomas Harte macrumors 6502

    Joined:
    Nov 30, 2005
    #10
    When you say it returns nil, is that from an NSLog or from checking with the debugger? The debugger is likely to show 'nil' if the optimiser has cut the variable.
     
  11. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #11
    Post all your code. Or at least more than one line. An isolated line of code is meaningless, without knowing what comes before and after.

    Also, are you building a command-line tool, or a bundled app? It's not clear from any prior post which one it is.
     
  12. kaydell.leavitt thread starter macrumors regular

    Joined:
    Apr 19, 2010
    #12
    OK. That's was it. I was seeing nil in the debugger, but in NSLog() it look OK.

    -- Kaydell :D
     

Share This Page