Amazon.com Widgets stringWithContentsOfFile returns (null) instead of the content
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 18, 2013, 02:29:24 PM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Programming in Objective-C, 4th edition
| |-+  Chapter 16
| | |-+  stringWithContentsOfFile returns (null) instead of the content
Pages: [1]   Go Down
Print
Author Topic: stringWithContentsOfFile returns (null) instead of the content  (Read 944 times)
MSchmidleitner
Newbie
*
Posts: 4






« on: July 04, 2012, 12:14:43 AM »

Hallo!
I have a little problem with Progam 16.1.
It all works.
Just the last statement, which should display the content of newfile2 doesn't work. It returns (null) instead of the content.
Does anybody have an idea, why?

Here is my code and the output:


//Program 16.1
//Basic file operations
//Assumes the existence of a file called "testfile"
//in the current directory

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    @autoreleasepool {
        NSString        *fName = @"/Users/michaela/Desktop/testfile";
        NSFileManager   *fm;
        NSDictionary    *attr;
       
        //Need to create an instance of the FileManager
        fm = [NSFileManager defaultManager];
       
        //let's make sure, our testfile exists, first
       
        if ([fm fileExistsAtPath:fName] == NO) {
            NSLog(@"File doesn't exist");
            return 1;
        }
       
        //now let's make a copy
        if ([fm copyItemAtPath:fName toPath:@"/Users/michaela/Desktop/newfile" error:NULL] == NO) {
            NSLog(@"File Copy failed!");
            return 2;
        }
       
        //now lets test to see if the two files are equal
        if ([fm contentsEqualAtPath:fName andPath:@"/Users/michaela/Desktop/newfile"] == NO) {
            NSLog(@"Files are not equal");
            return 3;
        }
       
        //now lets rename the copy
        if ([fm moveItemAtPath:@"/Users/michaela/Desktop/newfile" toPath:@"/Users/michaela/Desktop/newfile2" error:NULL] == NO) {
            NSLog(@"Files rename failed");
            return 4;           
        }
       
        //get the size of the newfile2
        if ((attr = [fm attributesOfItemAtPath:@"/Users/michaela/Desktop/newfile2" error:NULL]) == nil) {
            NSLog(@"Couldn't get file attributes");
            return 5;
        }
       
        NSLog(@"file size is %llu bytes",[[attr objectForKey:NSFileSize] unsignedLongLongValue]);
       
        //and finally let's delete the original file
        if ([fm removeItemAtPath:fName error:NULL] == NO) {
            NSLog(@"file removal failed");
            return 6;
        }
       
        NSLog(@"All operations were successfull");
 
       
        //display the content of the newly created file: DAS FUNKTIONIERT LEIDER NICHT
        NSLog(@"%@", [NSString stringWithContentsOfFile:@"/Users/michaela/Desktop/newfile2" encoding:NSUTF8StringEncoding error:NULL]);
       
    }   
    return 0;
}


Output:
2012-07-04 09:11:57.139 neues Uebungsprojekt[514:707] file size is 8180 bytes
2012-07-04 09:11:57.151 neues Uebungsprojekt[514:707] All operations were successfull
2012-07-04 09:11:57.152 neues Uebungsprojekt[514:707] (null)
Program ended with exit code: 0

Thanks for your help
Michi
Logged
symanski
Newbie
*
Posts: 4






« Reply #1 on: July 04, 2012, 03:35:11 AM »

I think it's something to do with permissions that you and your program have.   Hence why best to run the program in the directory that Xcode puts the files in to.

That path is something like:
/Users/john/Library/Developer/Xcode/DerivedData/prog-16-1-btbvqohqwdnkpbajxbrzjnvhpege/Build/Products/Debug

For me that's where it is, obviously your own username would be there and as for the random characters they'll be different too!   Give it a try at this location and it will probably work.

FYI - I can't get it to change directories outside this for what reason I don't know.
Logged
MSchmidleitner
Newbie
*
Posts: 4






« Reply #2 on: July 10, 2012, 04:52:29 AM »

Thanks for your held.

I tried to do, what you said, but the problem is, that I can't create the testfile in the Xcode-directory, because I can't find the directory. I know the path. It's similar to yours but I can't find it ...
I am normally a windows-user and new on the mac, that's why those things are a little bit difficult for me ...

Does anyone have an idea for me?
Logged
MSchmidleitner
Newbie
*
Posts: 4






« Reply #3 on: July 12, 2012, 08:20:14 AM »

Now it works.
I copied the testfile from desktop to the current directory
(in my case /Users/michaela/Library/Developer/Xcode/DerivedData/neues_Uebungsprojekt-eytaztreqgdmstcdwvticpbhduob/Build/Products/Debug/)
via Terminal.

Following points were necessary on the terminal
1) go to the desktop via cd .. or cd directoryName to move to the upper directory or a certain directory
2) enter following command for copy:
cp testfile /Users/michaela/Library/Developer/Xcode/DerivedData/neues_Uebungsprojekt-eytaztreqgdmstcdwvticpbhduob/Build/Products/Debug/testfile

3) now the program works ...

Thanks for your help!
Logged
symanski
Newbie
*
Posts: 4






« Reply #4 on: July 16, 2012, 03:28:27 AM »

MSchmidleitner,
glad you've got it working now.   Mac OS is quite different if you've only used Windows before, but it's essentially like Unix which I've had to use a few times with HP workstations etc.

I have to say that I'm liking Mac OS more than Windows now, and fear what Windows 8 will look like with just a tablet interface!   That's fine for a tablet, but not for when you're actually trying to use a PC to create programs or use CAD as I do.

Logged
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.