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 7920915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:23:31+00:00 2026-06-03T16:23:31+00:00

I want to overwrite the current data at the beginning of the my text

  • 0

I want to overwrite the current data at the beginning of the my text file called data.txt (in my resources folder). There will always be only one line (no spaces or anything) and I want this line to be overwritten when appropriate. This is how I have been doing it so far but this only writes contents at the end of the file and doesn’t begin from the beginning aka overwriting the line I need. If anyone can help me it would be appreciated. Thanks!

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0];

//append filename to docs directory
NSString *myPath =  [documentsDirectory stringByAppendingPathComponent:@"data.txt"];
fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:myPath];

writtenString= [[NSString alloc]initWithFormat:@"%d", message];     

[fileHandle seekToEndOfFile];
[fileHandle writeData:[writtenString dataUsingEncoding:NSUTF8StringEncoding]];

[writtenString release];
  • 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-03T16:23:34+00:00Added an answer on June 3, 2026 at 4:23 pm

    Mike’s answer will allow you to overwrite the contents of your file, but not in a nice line-oriented manner that you’re imagining.

    You can’t adjust the internal contents of a file such that, if you insert stuff, the existing following data will be pushed later and, if you remove stuff, the existing following data will be pulled up to come right after the removal point.

    You have two basic operations available: you can truncate a file at a certain length, possibly 0, discarding any old data past that point. And you can write data at a certain position. If there was already data at that position, it is overwritten (i.e. lost). If the byte range to which you’re writing extends past the end of the file, the file is extended.

    However, any of the old data which you neither truncated nor overwrote remains where it is. In no case is it moved.

    So, if you want to change the first line of a text file, and if you can’t be sure that the new line is exactly the same length as the old line, then you have to take care of moving everything from the second line on to its new location. One simple approach, suitable for small files, is to read the entire thing into memory, adjust it there, and write it all out again. Another is to iterate over the file, reading in chunks and then writing them out in their new locations. If you are moving data toward the start, you start at the beginning; if you are moving data toward the end, you start at the end.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm deploying a Wordpress blog, but I don't want to overwrite the current site
If you want to overwrite a file with Bash, this is easy echo Hello
I want to declare three properties in my MSBuild file and overwrite one property
I want to parse, process and then overwrite only 1 specific column in a
My known data are my current location (GeoPoint) and radius in meters. I want
I have managed to write to a text file but I want to be
I have some read-only data that I want to initialise and then re-initialise periodically
I want to find current location and compare it with some data in my
I am in the situation where I want to overwrite the set method that
Given two integers X and Y, I want to overwrite bits at position P

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.