当前位置: 动力学知识库 > 问答 > 编程问答 >

iphone - XML Tag xcode save file

问题描述:

I would like to output tags for each line with different tag names and want to save each in a file. I can write the file, but it is over writing. How do I write each and every line in a different file?

Input:

english titanic

leondradicapro kate

German Hotel hüßßan

tomhanks angleina

Output:

<language>english<language/>

<movie>titanic<movie/>

<Actor>leondradiacpro<Actor/>

<Actress>kate<Actress/>

<language>German<language/>

...

Code:

 for (NSString *str in lineFile)

{

if([str hasPrefix:@"english "])

{

NSMutableArray *dd = [[NSMutableArray alloc] initWithArray:[[str stringByReplacingOccurrencesOfString:@"english" withString:@""] componentsSeparatedByString:@" "]];

NSArray *tag = [NSArray aarrayWithObjects:@"er",@"langauge",@"movie",@"actor",@"kate",nil];

NSMutableString *output =[[NSMutableString alloc] init];

NSUInteger tagindex =0;

for (NSString *words in word)

{

if(tagindex >=[tag count])

{

NSLog(@"dad");

break;

}

[output appendFormat:@"<%@>%@<%@>\n", [tag objectAtIndex:tagindex],words,[tag objectAtIndex:tagindex]];

NSLog(@"%@",output);

[output writeToFile:@"/Users/xx/Desktop/homework.txt" atomically:YES encoding:NSASCIIStringEncoding error:NULL];

tagindex++;

now i can able to read only the language and movie (1st line) how i can add second line and tag also

网友答案:

you need write file with c method, maybe the following method can help you

-(void)writeToFile:(NSString *)str:(NSString *) fileName
{
    NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];
    NSString *filePath=[NSString stringWithFormat:@"%@",fileName];
    if([[NSFileManager defaultManager] fileExistsAtPath:filePath] == NO){
        NSLog(@"file not exist,create it...");
        [[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil];
    }else {
                NSLog(@"file exist!!!");
    }

    FILE *file = fopen([filePath UTF8String], [@"ab+" UTF8String]);

    if(file != NULL){
        fseek(file, 0, SEEK_END);
    }
    int readSize = [data length];
    fwrite((const void *)[data bytes], readSize, 1, file);
    fclose(file);
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: