Can't write a program, how to solve this error ?

Discussion in 'Mac Programming' started by master12, Mar 21, 2012.

  1. master12 macrumors newbie

    Mar 21, 2012
    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");


    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
  2. subsonix macrumors 68040

    Feb 2, 2008
    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.
  3. master12 thread starter macrumors newbie

    Mar 21, 2012
    Can you please help me with that ?
    Can you please show me how to do that ?
  4. robvas macrumors 68030

    Mar 29, 2009
    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

    Why reading strings can be a security issue:
  5. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    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:
    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.
  6. master12 thread starter macrumors newbie

    Mar 21, 2012
    I'm new and I just wanted to make something... I still don't know about function fgets()
  7. Sydde macrumors 68020


    Aug 17, 2009
    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.

Share This Page