PDA

View Full Version : Can't write a program, how to solve this error ?




master12
Mar 21, 2012, 06:34 AM
I want to make program, that will ask which file's name to rename.


When I write this code:

#import <Foundation/Foundation.h>

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

@autoreleasepool {

NSString *filename;
NSFileManager *changer;

NSLog(@"Type which file you want to rename");

scanf("%i",&filename);

changer = [NSFileManager defaultManager];

if([changer moveItemAtPath: filename toPath: @"go" error:NULL]==NO)
{ NSLog(@"Cant rename"); return 1; }


}
return 0;
}

I get this error:


2012-03-21 15:30:34.088 ChangeName[1091:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSFileManager moveItemAtPath:toPath:error:]: source path is nil'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff9561a286 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff8db76d5e objc_exception_throw + 43
2 CoreFoundation 0x00007fff9561a0ba +[NSException raise:format:arguments:] + 106
3 CoreFoundation 0x00007fff9561a044 +[NSException raise:format:] + 116
4 Foundation 0x00007fff93148d30 -[NSFileManager moveItemAtPath:toPath:error:] + 107
5 ChangeName 0x0000000100000e05 main + 197
6 ChangeName 0x0000000100000d34 start + 52
7 ??? 0x0000000000000001 0x0 + 1
)
terminate called throwing an exceptionsharedlibrary apply-load-rules all
Current language: auto; currently objective-c
(gdb)



subsonix
Mar 21, 2012, 07:28 AM
NSString can't be used as an argument to scanf(), you need to read into a regular C string (char array) then convert that to a NSString.

master12
Mar 21, 2012, 08:42 AM
NSString can't be used as an argument to scanf(), you need to read into a regular C string (char array) then convert that to a NSString.

Can you please help me with that ?
Can you please show me how to do that ?

robvas
Mar 21, 2012, 08:49 AM
Can you please help me with that ?
Can you please show me how to do that ?
Reading strings from the keyboard in C gets hairy, really quickly.


char YourName [50] ;
scanf ( "%s", YourName ) ;
printf ( "Hello %s\n", YourName ) ;

Then to convert it to NSString:

NSString *stringFromUTFString = [[NSString alloc] initWithUTF8String:utf8String];


More information

http://en.wikipedia.org/wiki/Scanf_format_string
https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/Articles/CreatingStrings.html
http://www.9wy.net/onlinebook/CPrimerPlus5/ch11lev1sec2.html

Why reading strings can be a security issue:

http://stackoverflow.com/questions/2977553/if-one-complains-about-gets-why-not-do-the-same-with-scanfs

chown33
Mar 21, 2012, 11:00 AM
Reading strings from the keyboard in C gets hairy, really quickly.

Use the fgets() function. You give it a limit on chars to read, i.e. the size of the buffer. It won't read more.

Once the string is read in, it's much safer and simpler to parse. There's a scan function that parses from strings. See the man page.


Also the OP's code is wrong in two ways:
scanf("%i",&filename);

1. %i doesn't read a string, but an integer.
2. filename can't be an NSString*.

The OP should work from a book or guided tutorial, instead of pasting together random pieces of code.

master12
Mar 21, 2012, 11:14 PM
Use the fgets() function. You give it a limit on chars to read, i.e. the size of the buffer. It won't read more.

Once the string is read in, it's much safer and simpler to parse. There's a scan function that parses from strings. See the man page.


Also the OP's code is wrong in two ways:
scanf("%i",&filename);

1. %i doesn't read a string, but an integer.
2. filename can't be an NSString*.

The OP should work from a book or guided tutorial, instead of pasting together random pieces of code.

I'm new and I just wanted to make something... I still don't know about function fgets()

Sydde
Mar 22, 2012, 01:11 AM
Well, I suppose you could try
NSString *inputString;
inputString = [[NSString alloc] initWithContentsOfFile:@"/dev/stdin"];
but I doubt it would work at all and it might be dangerous.