开发者

Creating tab delimited file in objective-c

开发者 https://www.devze.com 2023-04-12 16:40 出处:网络
I found this snippet online to write, and then append data to a text file: - (void)appendText:(NSString *)text toFile:(NSString *)filePath {

I found this snippet online to write, and then append data to a text file:

- (void)appendText:(NSString *)text toFile:(NSString *)filePath {

    // NSFileHandle won't create the file for us, so we need to check to make sure it exists
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:filePath]) {

        // the file doesn't exist yet, so we can just write out the text using the 
        // NSString convenience method

        NSError *error = noErr;
        BOOL success = [text writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
        if (!success) {
            // handle the error
            NSLog(@"%@", error);
        }

    } 
    else {

        // the file already exists, so we should append the text to the end

        // get a handle to the file
        NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];

        // move to the end of the file
        [fileHandle seekToEndOfFile];

 开发者_开发知识库       // convert the string to an NSData object
        NSData *textData = [text dataUsingEncoding:NSUTF8StringEncoding];

        // write the data to the end of the file
        [fileHandle writeData:textData];

        // clean up
        [fileHandle closeFile];
    }
}

This makes sense to me. I have a class that has 3 properties, of NSString, NSInteger, and NSString. When I try to use this method, I do this:

for (MyObject *ref in array) {
    NSString *stringToFile = [NSString stringWithFormat:@"%@\t%i\t%@", ref.ChrID, ref.Position, ref.Sequence];
    [self appendText:stringToFile toFile:filePath];
}

It doesn't look quite right. My data looks like this:

NSString *tab* NSInteger *single space* NSStringNSString *tab* NSInteger newline
NSStringNSString *tab* NSInteger newline 
NSStringNSString *tab* NSInteger newline
NSStringNSString *tab* NSInteger newline
NSStringNSString *tab* NSInteger newline
NSStringNSString *tab* NSInteger newline
NSStringNSString *tab* NSInteger newline
NSStringNSString *tab* NSInteger newline
...

I'm not sure what is going on to make it look like this. When I NSLog the data, it looks fine. But something with the first line gets messed up, and then everything seems to be off. Any thoughts? Thanks.


There are several issues with the method appendText:

  • if the file does not exist, the first line is written with the NSString writeToFile method without the \n

  • following lines are written with the NSData writeData method

  • it's very inefficient to use a filemanager to check for existence, get a filehandle, seek to EOF and then write just one line, omitting the close too. And repeating this for every following line.

So better do it this way:

  • get the filehandle for writing, it will be created if it's not there yet

  • seek to EOF

  • do your loop with writeData for each line

  • close the file

0

精彩评论

暂无评论...
验证码 换一张
取 消