Need help - can't find FILE!


#1

I did the challenge by creating a class called Family , populating it with the necessary elements and then writing to a file. The program builds and runs fine. The problem is the file never gets created! I use go…to…tmp in finder and it says “folder not found” - i’ve tried other paths and --no file - what did I do wrong?

main.m



#import <Foundation/Foundation.h>
#import "Family.h"

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        

        
        //Set values for first member of Family
        Family *Manny = [[Family alloc]init];
        [Manny setName:@"Manny"];
        [Manny setHeight:6.1];
        [Manny setDOB:[NSDate dateWithString:@"11/08/1971"]];
        [Manny setWeight:220];
        [Manny setIsInsured:YES];
        NSArray *top3movies = [[NSArray alloc] initWithObjects:@"Pandora",@"Star Trek",@"Godfather", nil];
        [Manny setTop3movies:top3movies];
        NSData *pic = [[NSData alloc] initWithContentsOfFile:@"/Users/Manny/Dropbox/My Pictures/manny happy shot.jpg"];
        [Manny setPicture:pic];
        NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"Marino",@"Football",@"Stanton",@"Baseball", nil];
        [Manny setFavorite_players:dict];
        
        //Set values for second member of family
        Family *Gilly = [[Family alloc]init];
        [Gilly setName:@"Gilly"];
        [Gilly setHeight:5.0];
        [Gilly setDOB:[NSDate dateWithString:@"10/07/1970"]];
        [Gilly setWeight:120];
        [Gilly setIsInsured:NO];
        NSArray *top3 = [[NSArray alloc] initWithObjects:@"The Notebook",@"MI3",@"The Matrix", nil];
        [Gilly setTop3movies:top3];
        NSData *pic2 = [[NSData alloc] initWithContentsOfFile:@"/Users/Manny/Dropbox/My Pictures/The Best for Show/_DSC2987.jpg"];
        [Gilly setPicture:pic2];
        NSDictionary *dict2 = [[NSDictionary alloc] initWithObjectsAndKeys:@"Marino",@"Football",@"none",@"Baseball", nil];
        [Gilly setFavorite_players:dict2];
        
        //Create master array to hold both members
        NSMutableArray *MyFamily = [[NSMutableArray alloc]init];
        [MyFamily addObject:Manny];
        [MyFamily addObject:Gilly];
        
        //Write proplist file
        [MyFamily writeToFile:@"/tmp/myfamily.plist" atomically:YES];
        
        NSLog(@"File written");
        
        
         
        
        
    }
    return 0;
}

family.h

[code]
#import <Foundation/Foundation.h>

@interface Family : NSObject
{
NSString *name;
NSData *picture;
NSDate *DOB;
NSArray *top3movies;
NSDictionary *favorite_players;
int weight;
float height;
BOOL IsInsured;
}

@property NSString *name;
@property NSData *picture;
@property NSDate *DOB;
@property NSArray *top3movies;
@property NSDictionary *favorite_players;
@property int weight;
@property float height;
@property BOOL IsInsured;

@end[/code]
family.m

#import "Family.h"

@implementation Family
@synthesize name,picture,DOB,top3movies,favorite_players,weight,height,IsInsured;

@end

#2

What did you type in when the Finder prompted you? tmp or /tmp

You should type in /tmp not tmp.

[color=#FF0000]/tmp[/color]/myfamily.plist

From the command line, too, check that the folder [color=#FF0000]/tmp[/color] exists.

To check:

  • Run Terminal Application;
  • Type in the command: ls /tmp and hit
    If the folder does not exist, ls will print the error message: ls: /tmp: No such file or directory

If the folder does exist and you don’t see your file there, then a possible reason might be that you don’t have permissions to write to /tmp. Change /tmp to ~/tmp and try again (after making sure that there is a tmp directory in your home directory.)


#3

Thanks for your reply. I tried typing /tmp instead and looked in Terminal - initially a query to /tmp pointed me to a directory under private - my files were not there. I tried manually creating the tmp directory in my Home folder and explicitly putting that in my code - no dice. I tried the “~” character and nothing. For kicks I put garbage in the pathname to write to in the program. The line happily executes with no compiler errors. - and of course no file. I guess my next step is to try to figure out if the program is even attempting to write the file.

Here’s the one line with my specific path and the ~

An other ideas?


#4

The path [color=#FF0000]~/Users/Manny[/color]/tmp/myfamily.plist is invalid.

However the path ~/tmp/myfamily.plist will be valid if ~/tmp (folder named tmp in your home dir) exists.

If you still can’t write to the tmp directory, you might have a sandboxing (permission) issue.


#5

Manny,

In the finder menu click on “Go” select “Go to folder”. Enter “/tmp”. It should take you right to the tmp folder. There is another tmp folder in the folder “Private”. Just enter /Private in the go to box to see it.

Mitch


#6

I did find the tmp under Private. The plist isn’t there. Heck, I’ve scoured my hard drive far and wide for any file named myfamily.plist and I can’t find it. How can I confirm if this is a permissions issue? BTW, Also, if someone can look over my code just to make sure i didn’t miss something important. I at least want to rule that out.

I appreciate everyone’s help in helping solve this mystery.


#7

Manny,

Change the line of code to this:

if ([MyFamily writeToFile:@"/tmp/myfamily.plist" atomically:YES] == YES)
NSLog(@“File written”);

else
    NSLog(@"File not written!");

You will see that the file is not being written.

Change [myFamily addObject:Manny} to a string object like [MyFamily addObject:@“Hello”];
and the same to addObject:Gilly to [MyFamily addObject:@“Hello again”];

Build and run, you should get “File written” this time. In Finder click “go” enter “/tmp” in the go to folder box and see the file there.
The objects Manny and Gilly are not usable with -writeToFile method.

Mitch


#8

THanks - you’re right - the file was never written.