파일 (hello.in)을 읽고 줄별로 다른 파일에 쓰기를 원합니다. 아래의 메소드를 작성하고 실행할 때 sigabrt 오류가 발생합니다. 나는 대답을 얻고 싶다.WriteData로 인해 sigabrt 오류가 발생합니다.
-(void) saveAsLineLine: (NSString*) fileName
{
NSString *filePath;
filePath = [NSString stringWithFormat:@"%@%@", pathdir, @"hello.in"];
// reading the file
NSString *entireFileInString = [NSString stringWithContentsOfFile:filePath encoding:(NSUTF8StringEncoding) error:nil];
// each line, adjust character for line endings
NSArray *lines = [entireFileInString componentsSeparatedByString:@"\n"];
filePath = [NSString stringWithFormat:@"%@%@", pathdir, fileName];
// create a file
[[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil];
// open the file for writeing
NSFileHandle *fh = [NSFileHandle fileHandleForWritingAtPath:filePath];
// write line by line
for (NSData *line in lines)
{
NSLog (@"line: %@\n", line);
[fh seekToEndOfFile];
[fh writeData: line]; // THE BUG IS HERE !!!!!!!!
// @try {
// [fh writeData: line];
//
// }
// @catch (NSException *exception) {
// NSLog(@"%@",[exception description]);
// }
}
[fh closeFile];
}
사용'-stringByAppendingPAthComponent는'파일 경로를 만들 수 있습니다. 디렉토리와 파일을 연결하는 것보다 훨씬 강력합니다. 디렉토리가 마지막'/'이 누락 되어도 상관 없습니다. – JeremyP