|
|
Append string to file in Cocoa
|
|
|
|
Addicted to MacNN
Join Date: Oct 2001
Location: Yokohama, Japan
Status:
Offline
|
|
In the app I'm working on, I need to append strings to a logfile, and I'm having a hard time figuring out how. It seems like every way I try overwrites the entire file instead of appending to it. Is there no simple equivalent of "echo $STRING >> file.log" in Cocoa?
|
|
|
|
|
|
|
|
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
See the open() function in the C standard library. If you don't have to be compatible pre-10.3, there's also NSOutputStream.
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
|
|
|
|
|
|
|
Junior Member
Join Date: Sep 2003
Location: Nowhere, Missouri
Status:
Offline
|
|
Why be difficult?
NSString *logfileString = [NSString stringWithContentsOfFile:@"/path/to/file.txt"];
logfileString = [logfileString stringByAppendingString:@"omglol"];
[logfileString writeToFile:@"/path/to/file.txt" atomically:YES];
|
|
|
|
|
|
|
|
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
It's conceivable that this logfile could be quite large. Reading the whole thing into memory, copying it and then writing it out again is quite wasteful, and would scale very poorly as the file size increases. Granted, the waste wouldn't matter much at smaller sizes, but I don't think such an inflexible solution would be the best one.
EDIT: Reading this in the morning, it sounds kind of harsh. I like the idea of using a simple solution. This one just seemed ill-suited to logfiles.
(
Last edited by Chuckit; Aug 10, 2005 at 02:15 PM.
)
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
|
|
|
|
|
|
|
Addicted to MacNN
Join Date: Oct 2001
Location: Yokohama, Japan
Status:
Offline
|
|
Thanks for the advice. I had found NSOutputStream, but I couldn't figure out how to work the write function.
The log file will likely grow to a decent size, so I won't use your suggestion, Arclite, but thanks anyway. It's a clever solution.
I ended up getting it to work with the method posted here. Here's my implementation:
Code:
NSFileHandle *output = [NSFileHandle fileHandleForWritingAtPath:logFilePath];
[output seekToEndOfFile];
[output writeData:[message dataUsingEncoding:NSUTF8StringEncoding]];
I had tried to do this before, but I couldn't figure out how to get my NSString into an NSData without the extra garbage that NSArchiver adds.
Now I just need to figure out how to insert a linebreak. \n doesn't seem to be working.
|
|
|
|
|
|
|
|
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
\n ought to work. What is it doing? Nothing?
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
|
|
|
|
|
|
|
Addicted to MacNN
Join Date: Oct 2001
Location: Yokohama, Japan
Status:
Offline
|
|
Oops. I thought I cancelled that edit to my post. \n did work. I don't know what the problem was when I first tried it.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Forum Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|