Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
528 views
in Technique[技术] by (71.8m points)

write data line by line on iphone

i can write to a text file on iphone..but each time i write my previous value is erased, is there any way to keep writing data separated by ??

this is my code

NSString *cc=@"1";

    [cc writeToFile:storePath atomically:YES];

    NSString *myText1 = [NSString stringWithContentsOfFile:storePath];

    NSLog(@"text is %@",myText1);

this gives me 1 now i want to add 2 like 1 and then 2

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use NSFileHandle method seekToEndOfFile and the call to writeData

NSFileHandle *aFileHandle;
NSString *aFile;

aFile = [NSString stringWithString:@"Your File Path"]; //setting the file to write to

aFileHandle = [NSFileHandle fileHandleForWritingAtPath:aFile]; //telling aFilehandle what file write to
[aFileHandle truncateFileAtOffset:[aFileHandle seekToEndOfFile]]; //setting aFileHandle to write at the end of the file

[aFileHandle writeData:[toBeWritten dataUsingEncoding:nil]]; //actually write the data

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...