Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

For my application, I need a user to be able to enter some input through the console, and that string, except uppercased, to be part of a path file. I have the following code, but while everything appears to run smoothly, the file isn't actually created. I don't even get the error message through my if block.

#define DEBUGGER 1
NSLog (@"Username:");
char userInput[70];
fgets (userInput, sizeof userInput, stdin);
int c;
while ((c = getchar()) != '\n' && c != EOF);
if (userInput [strlen(userInput) - 1] == '\n') //In case the input string has # characters plus \n
    userInput[strlen(userInput) - 1] = '\0'; //Plus '\0', the '\n' isn't added and the if condition is false
NSFileManager * fm = [NSFileManager defaultManager];
NSString * string = [NSString stringWithUTF8String: userInput];
NSString * stringUppercase = [string uppercaseString];
NSString * dirUsername = [NSString stringWithFormat:@"~/Desktop/ProjAlleleData/Accounts/%@", stringUppercase.stringByExpandingTildeInPath];stringByExpandingTildeInPath];
#ifdef DEBUGGER
NSLog(@"DEBUGGER MESSAGE: Username Directory Path: %@", dirUsername);
#endif
if ([fm createDirectoryAtPath: dirUsername withIntermediateDirectories: YES attributes: nil error: NULL] != YES) {
    NSLog(@"Save File Creation Error.");
    return 1;}
share|improve this question
I think the error (now that I included @Josh's) has to do with the pathname not being expanded properly... (I bet I'm going to find a whole bunch of randomly named directories in some folder close to root someday :P) – jazz14456 Jul 29 at 18:43
Confirmed that the error is about the tilde not being expanded in the pathname... I just replaced the tilde with /Users/whateverMyUserIsCalled and it worked. I do need the tilde to be expanded though. – jazz14456 Jul 29 at 18:47
add comment (requires an account with 50 reputation)

1 Answer

up vote 0 down vote accepted
NSString * dirUsername = [@"~/Desktop/MyFiles/%@", stringUppercase stringByExpandingTildeInPath];

should be:

NSString *dirUsername = [NSString stringWithFormat:@"~/Desktop/MyFiles/%@", stringUppercase];
NSString *dirUsername2 = [dirUsername stringByExpandingTildeInPath];

or

NSString *dirUsername = [[NSString stringWithFormat:@"~/Desktop/MyFiles/%@, stringUppercase] stringByExpandingTildeInPath];

Your version was creating an array of the format string and the components, and assigning the array to dirUsername, but this version uses NSString stringWithFormat:.

(stringByAppendingPathComponent may be better, though: [[@"~/Desktop/MyFiles" stringByAppendingPathComponent:stringUppercase] stringByExpandingTildeInPath];)

share|improve this answer
I get an error pointing at stringByExpandingTildeInPath that says "expected :" when I do the solution you suggested. – jazz14456 Jul 29 at 17:05
Sorry, try that. It was stringUppercase stringByExpandingTildeInPath, which was missing a :, so it could be either stringUppercase.stringByExpandingTildeInPath or [stringUppercase.stringByExpandingTildeInPath]. – Josh The Geek Jul 29 at 17:10
I accepted your answer :). For future users you might want to take out the period in your last statement (it is either [receiver message] or reciever.message, not both. – jazz14456 Jul 29 at 17:30
I'll accept your answer, but please add in this line: NSString * dirUsername2 = [dirUsername stringByExpandingTildeInPath]; for future users, and remove the stringByExpandingTildeInPath from the first dirUsername. This is the only way to let the path have input AND expand the tilde that I could make work. – jazz14456 2 days ago
Updated with your suggestions – Josh The Geek 2 days ago
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.