我写了这个程序但无法得到输出,每次运行这个程序时我都得到唯一的第一个 NSlog
语句。我已经在此路径和内容中创建了我的文件。
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool
{
NSString *fName = @"/Users/sahil/Documents/newfile/commands";
NSFileManager * fm;
NSDictionary *attr;
fm=[NSFileManager defaultManager];
if ([fm fileExistsAtPath: fName]== NO)
{
NSLog(@"file not exist");
return 1;
}
if ([fm copyItemAtPath:fName toPath:@"newfile1" error:NULL] == NULL)
{
NSLog(@"file copy failed");
return 2;
}
if ([fm contentsEqualAtPath:fName andPath:@"newfile1"]==NO)
{
NSLog(@"files are not =");
return 3;
}
if ([fm moveItemAtPath:@"newfile1" toPath:@"newfile2" error:NULL]== NULL)
{
NSLog(@"file rename fail");
return 4;
}
if ((attr=[fm attributesOfItemAtPath:@"newfile2" error:NULL])==nil)
{
NSLog(@"couldnt get file attributes");
return 5;
}
NSLog(@"file size is %llu bytes",[[attr objectForKey:NSFileSize] unsignedLongLongValue]);
if ([fm removeItemAtPath:fName error:NULL] == NULL)
{
NSLog(@"file removal failed");
return 6;
}
NSLog(@"all ops were successful");
NSLog(@"%@",[NSString stringWithContentsOfFile:@"newfile2" encoding:NSUTF8StringEncoding error:NULL]);
}
return 0;
}
请帮我解决这个问题。 谢谢
最佳答案
操作码:
if ([fm fileExistsAtPath: fName]== NO)
{
NSLog(@"file not exist");
return 1;
}
如果 NSLog
打印,将返回,因此程序将不再执行。
删除 return 1;
语句。
这假设“我得到了唯一的第一个 NSlog 语句。”表示语句:NSLog(@"file not exist");
执行。
操作码:
if ([fm copyItemAtPath:fName toPath:@"newfile1" error:NULL] == NULL)
{
NSLog(@"file copy failed");
return 2;
}
添加错误日志:
NSError *error;
if ([fm copyItemAtPath:fName toPath:@"newfile1" error:&error] == NULL)
{
NSLog(@"file copy failed, error: %@", error);
return 2;
}
关于objective-c - 对文件的一些操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21853926/