Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8394303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:01:27+00:00 2026-06-09T20:01:27+00:00

I have managed to write to a text file but I want to be

  • 0

I have managed to write to a text file but I want to be able to add more data to the text file without overwriting the current contents of the text file. So in other words, I want to write to the end of the file each time so it doesn’t erase current content. The following code is what I have done so far but it overwrites every time and writes to the beginning, I am not sure how to get it write to the end. Based on this code, the current content in the textfile looks like:

June
July
August
September

I want to be able to add the following for example

 June
 July
 August
 September
 October

Does anyone know how to this? Thanks!

-(void) writeToTextFile{
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName = [NSString stringWithFormat:@"%@/textfile.txt", 
                      documentsDirectory];
//create content - four lines of text
NSString *content = @"June\nJuly\nAugust\nSeptember";
//save content to the documents directory
[content writeToFile:fileName 
          atomically:NO 
            encoding:NSStringEncodingConversionAllowLossy 
               error:nil];

 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T20:01:29+00:00Added an answer on June 9, 2026 at 8:01 pm

    Use this method to write at End of file:

    -(void) writeToTextFileWithString:(NSString *) content name:(NSString *) file {
    
      //TODO: Disable logs at release time
    
      content = [NSString stringWithFormat:@"%@ |||   %@\n",content,[[NSDate date] description]];
    
      //get the documents directory:
      NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
      NSString *fileName = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.txt",file]];
    
      NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:fileName];
      if (fileHandle){
    
        [fileHandle seekToEndOfFile];
        [fileHandle writeData:[content dataUsingEncoding:NSUTF8StringEncoding]];
        [fileHandle closeFile];
      }
      else{
    
        [content writeToFile:fileName
                  atomically:NO
                    encoding:NSStringEncodingConversionAllowLossy
                       error:nil];
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My application creates a text file. I want that users to have easy access
I have a bunch of URL's in a text file but I'm only interested
So far I have managed to write some code that should print the source
I have an XML file with property groups and properties. I want to retrieve
I'm creating a class to manage text files. I have a method to write
I have xml-file. I need to read it, make some changes and write new
In my current project, I have to create a PDF file on the fly
I have managed to write a jquery to do the following: Upon selecting a
I have to write a utility to enumerate and manage the owners of groups
I have managed to extract the filename and the extension and passed that to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.