Amazon.com Widgets 16.1
Welcome, Guest. Please login or register.
Did you miss your activation email?
December 20, 2014, 06:05:29 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
|-+ Old Stuff
| |-+ Program Examples
| | |-+ Chapter 16
| | | |-+ 16.1
Pages: [1] 2 Go Down
Print
Author Topic: 16.1 (Read 4990 times)
sir
Full Member
***
Posts: 118


Email




on: April 10, 2009, 05:37:20 PM

I tried typing the program example 1 in as follows, but I get the error "invalid lvalue in assignement".  I'm not sure what I did wrong.  I made my "testfile" with TextEdit and saved it without an extension.  Maybe I should have done that a different way...
Code: (Objective-C)
//Basic file operations
//Assumes the existence of a file called "testfile"
//in the current working directory

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString *fName = @"testfile";
NSFileManager *fm;
NSDictionary *attr;

//Need to create an instance of the file manager
fm = [NSFileManager defaultManager];

//Let's make sure our test file exists
if ([fm fileExistsAtPath: fName] = NO) {
NSLog(@"File does not exist!");
return 1;
}

//Now let's make a copy
if ([fm copyPath: fName toPath: @"newfile" handler: nil] == NO) {
NSLog (@"File copy failed!");
return 2;
}

//Let's test to see if the two files are identical

if ([fm contentsEqualAtPath: fName andPath: @"newfile"] == NO) {
NSLog(@"Files are not equal!");
return 3;
}

//Now, let's rename the copy.

if ([fm movePath: @"newfile" toPath: @"newfile2" handler: nil] ==NO) {
NSLog(@"File rename failed!");
return 4;
}

//Get the size of newfile2
if ((attr = [fm fileAttributesAtPath: @"newfile2" traverseLink: NO]) == nil) {
NSLog(@"Couldn't get file attributes!");
return 5;
}

NSLog (@"File size is %i bytes", [[attr objectForKey: NSFileSize] intValue]);

//And finally, let's delete the original file
if ([fm removeFileAtPath: fName handler: nil] == NO) {
NSLog(@"File removal failed!");
return 6;
}

NSLog(@"All operations were successful!");

//Display the contents of the newly-created file
NSLog(@"%@", [NSString stringWithContentsOfFile: @"newfile2" encoding: NSUTF8StringEncoding error: nil]);

    [pool drain];
    return 0;
}

Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #1 on: April 10, 2009, 06:02:13 PM

Code: (Objective-C)
	if ([fm fileExistsAtPath: fName] = NO) {

One = for assigment, two == for equality test.

You should learn to use the compiler's line numbers to guide you to the source of the error.  XCode will automatically mark the lines in question for you.

Cheers,

Steve Kochan
Logged
sir
Full Member
***
Posts: 118


Email




Reply #2 on: April 11, 2009, 11:49:05 AM

whoops, sorry, i should have caught that.  i'll look more carefully next time...

In regards to testfile, how should we create it?  I made it using TextEdit and saved it without an extension.  I double checked and the extension is gone.  The program returns 1, so it does not see testfile, but it is there in the directory the project is in.
Last Edit: April 11, 2009, 11:52:50 AM by sir Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #3 on: April 11, 2009, 01:49:29 PM

Look at the note on Page 382.

Steve
Logged
sir
Full Member
***
Posts: 118


Email




Reply #4 on: April 11, 2009, 05:47:55 PM

Hmm... I don't think that was it.  I tried it that way and then I even retyped the program on another project.  It still returns 1.  The testfile is in the project...  There must be something else wrong.

Thanks for hangin in there with my incessant questions.
Code: (Objective-C)
//Basic file operations
//Assumes the existence of a file called "testfile"
//in the current working directory
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString *fName = @"testfile";
NSFileManager *fm;
NSDictionary *attr;

//Need to create an instance of the file manager
fm = [NSFileManager defaultManager];

//Let's make sure our test file exists first
if ([fm fileExistsAtPath: fName] == NO) {
NSLog (@"File doesn't exist!");
return 1;
}

//Now let's make a copy.
if ([fm copyPath: fName toPath: @"newfile" handler: nil] ==NO) {
NSLog (@"File copy failed.");
return 2;
}

//Let's test to see if the two files are identical.
if ([fm contentsEqualAtPath: fName andPath: @"newfile"] == NO) {
NSLog(@"Files are not equal!");
return 3;
}

//Now let's rename the copy
if ([fm movePath: @"newfile" toPath: @"newfile2" handler: nil] == NO) {
NSLog(@"File rename failed!");
return 4;
}

//Get the size of newfile2
if ((attr = [fm fileAttributesAtPath: @"newfile2" traverseLink: NO]) == nil) {
NSLog(@"Couldn't get the file attributes!");
return 5;
}

NSLog(@"File size is %i bytes", [[attr objectForKey: NSFileSize] intValue]);

//ANd finally, let's delete the original file
if ([fm removeFileAtPath: fName handler: nil] == NO) {
NSLog(@"File removal failed!");
return 6;
}

NSLog(@"All operations were successful!");

//Display the contents of the newly-created file
NSLog(@"%@", [NSString stringWithContentsOfFile: @"newfile2" encoding: NSUTF8StringEncoding error: nil]);

    [pool drain];
    return 0;
}


Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #5 on: April 11, 2009, 05:59:29 PM

Please insert this line into your program before the first if statement and copy the output from the Debugger Console into your reply:

Code: (Objective-C)
	system ("pwd; ls -l");

I still think the file is in the wrong directory; this will ascertain if that's the case.

Cheers,

Steve
Logged
sir
Full Member
***
Posts: 118


Email




Reply #6 on: April 11, 2009, 06:28:28 PM

Ah, I figured it out! 

I took it out of the project directory and put it in build/Release/

That's the directory  system ("pwd; ls -l"); looked in.

Now it works!

Thanks!

Nicholas
Logged
poco
Newbie
*
Posts: 3






Reply #7 on: May 03, 2009, 06:53:41 AM

Please insert this line into your program before the first if statement and copy the output from the Debugger Console into your reply:

Code: (Objective-C)
	system ("pwd; ls -l");

I still think the file is in the wrong directory; this will ascertain if that's the case.

Cheers,

Steve


system("pwd; ls -l"); shows the system is looking at /users/name directory. how can i change it to make it look into build/debug or build/release directory?
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #8 on: May 03, 2009, 11:53:50 AM

Look at Project->Edit Project Settings

Look at the "Place Build Products In:"

Look also at the Build Location settings under the Build tab.

Cheers,

Steve Kochan
Logged
poco
Newbie
*
Posts: 3






Reply #9 on: May 03, 2009, 07:22:55 PM

Look at Project->Edit Project Settings

Look at the "Place Build Products In:"

Look also at the Build Location settings under the Build tab.

Cheers,

Steve Kochan

thanks Steve,
                     i have another question. it seems that i have to open a terminal manually to execute the executive file (./executable) before the program runs correctly. using finder to locate the executable file and double clicking on it does not work. is that normal?
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #10 on: May 04, 2009, 04:20:23 AM

Yes, this program is designed to run from a shell, that is from within the Terminal application. 

Cheers,

Steve
Logged
lotusman
Newbie
*
Posts: 13






Reply #11 on: July 20, 2009, 04:05:48 PM

Hope this helps others if they get the same error I got when compiling: The codeline below has a missing comma after
@"%@",

Program 16.1

//Display the contents of the newly-created file
      NSLog(@"%@", [NSString stringWithContentsOfFile: @"newfile2" encoding: NSUTF8StringEncoding error: nil]);
Logged
wellaged37
Newbie
*
Posts: 39






Reply #12 on: August 09, 2009, 08:30:00 AM

I'm using XCode 3.1.
If the active configuration is Release and "testfile" is in Chapter 16/Program 16.1/build/Release then Build/Go is successful and I can even use breakpoints.
However, if the active configuration is Debug and "testfile" remains in the above location, then I get return 1.
If I now move "testfile" to Chapter 16/Program 16.1/build/Program 16.1/build/Debug, then I still get return 1.
Even if I move it to the folder Chapter 16.1 which also holds .m, I get return 1.

I looked  at Project->Edit Project Settings and Place Build Products In: and at the Build Location settings under the Build tab, but I don't know how I should edit them so that if I place "testfile" in folder Chapter 16.1 (the one with .m in it) then I will get return 0.

I hope this is clear.
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #13 on: August 09, 2009, 10:23:10 AM

Insert the system call I indicated in one of the above posts into your program to see what directory the application is running in.  That's the directory you need to put testfile in.

Cheers,

Steve Kochan
Logged
wellaged37
Newbie
*
Posts: 39






Reply #14 on: August 09, 2009, 12:44:40 PM

Steve,
I did as you suggested and this is what I got.

[Session started at 2009-08-09 15:35:15 -0400.]
/Users/johncarswell/Programming/Cocoa(ObjC)/StephenKochan/MyWorkbook/Chapter 16/Program 16.1/build/Debug
total 40
drwxr-xr-x  3 johncarswell  admin    102 Aug  8 21:04 Debug
-rwxr-xr-x  1 johncarswell  admin  17404 Aug  9 15:35 Program 16.1
drwxr-xr-x@ 5 johncarswell  admin    170 Aug  8 21:09 Program 16.1.build
2009-08-09 15:35:15.924 Program 16.1[173:10b] File doesn't exist!

The Debugger has exited with status 1.The Debugger has exited with status 1.

How can I tell from this which directory the application is running in?
Logged
Pages: [1] 2 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.