Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Append string to file in Cocoa

Append string to file in Cocoa
Thread Tools
wataru
Addicted to MacNN
Join Date: Oct 2001
Location: Yokohama, Japan
Status: Offline
Reply With Quote
Aug 9, 2005, 10:38 PM
 
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?
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Aug 10, 2005, 03:11 AM
 
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'."
     
Arclite
Junior Member
Join Date: Sep 2003
Location: Nowhere, Missouri
Status: Offline
Reply With Quote
Aug 10, 2005, 03:29 AM
 
Why be difficult?

NSString *logfileString = [NSString stringWithContentsOfFile:@"/path/to/file.txt"];
logfileString = [logfileString stringByAppendingString:@"omglol"];
[logfileString writeToFile:@"/path/to/file.txt" atomically:YES];
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Aug 10, 2005, 03:41 AM
 
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'."
     
wataru  (op)
Addicted to MacNN
Join Date: Oct 2001
Location: Yokohama, Japan
Status: Offline
Reply With Quote
Aug 10, 2005, 01:50 PM
 
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.
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Aug 10, 2005, 02:39 PM
 
\n ought to work. What is it doing? Nothing?
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
wataru  (op)
Addicted to MacNN
Join Date: Oct 2001
Location: Yokohama, Japan
Status: Offline
Reply With Quote
Aug 10, 2005, 02:40 PM
 
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 Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 10:41 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,