对于我的应用程序,我需要用户能够通过控制台输入一些输入,并且该字符串(大写除外)成为路径文件的一部分。我有以下代码,但虽然一切似乎运行顺利,但实际上并没有创建该文件。我什至没有通过我的 if 块收到错误消息。
#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;}