I want to read a file (hello.in) and to write it to another file line by line.
I write the below method and when I run it I get sigabrt error.
I would love to get answer.
-(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];
}
The
NSArraylinesholds instances ofNSString, notNSData, right?If you want to write data to file…