How to add text to the end of a text file?

Discussion in 'iOS Programming' started by HappyDungcicle, Sep 27, 2010.

  1. HappyDungcicle macrumors newbie

    Joined:
    Sep 27, 2010
    #1
    Hello,


    I am HappyDungcicle. Happy Dungcicle is confused. How does Happy Dungcicle find an easy way to add text to the end (or to the middle) of a text file? Happy Dungcicle keeps his/her/its text file in the "MainBundle" (I think?). Here is the code I have that works that reads and displays the text file:
    Code:
    - (void)viewDidLoad {
        [super viewDidLoad];
    	
    	NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Tacos"ofType:@"txt"];
    	
    	if (filePath)
    	{
    		NSString *textFromFile = [NSString stringWithContentsOfFile:filePath];
    		
    		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Help Message"
    														message:textFromFile
    													   delegate:self
    											  cancelButtonTitle:@"OK"
    											  otherButtonTitles: nil];
    		[alert show];
    		[alert release];
    		
    	}
    }

    Happy Dungcicle would like to be able to add text to the end of this text file, without erasing the text that is already there. How does Happy Dungcicle do this? Happy Dungcicle is good at programming other languages but not Iphone. So precise code would be very helpful.
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    NSString, which you've already loaded your text file into, supports a few appending methods.
     
  3. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #3
    I don't believe you can as the application bundle is read-only and shouldn't allow its contents to be modified.
     
  4. HappyDungcicle thread starter macrumors newbie

    Joined:
    Sep 27, 2010
    #4
    Then it sounds like Happy Dungcicle will be forced to read and write data from the documents folder. Happy Dungcicle wonders if there is a way to add text files directly to an app's documents folder rather than being forced to add such text files programmaticlly?

    Thanks, Happy Dungcicle
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    There isn't. But depending on what you are using this text file for (say, for example, a data store), there may be other approaches you can consider. So, if you elaborated on your needs / uses for the info contained in the file, hopefully we can provide better advice.

    P.S. Please refrain from referring to yourself in the third person. It just makes you sound either pretentious or like a house-elf.
     
  6. HappyDungcicle thread starter macrumors newbie

    Joined:
    Sep 27, 2010
    #6
    While I appreciate the answers I have gotten, I have a better idea. How about if Happy Dungcicle talks in the third person if Happy Dungcicle wants to? When someone tries to control the way Happy Dungcicle expresses him/her/itself, Happy Dungcicle gets the idea that the person doing the suggesting is so insecure that they feel the need to go so far as to try to control the way other people ask questions. I would rather be pretentious than a control-freak. :apple:

    Thanks! Happy Dungcicle.
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    Alright. Sorry for the suggestion. Hope HappyDungcicle finds the help that HappyDungcicle seems so desperate to get. I'm out.
     
  8. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #8
    What is this text file used for? The answer will help us in directing you do a useful solution.
     
  9. (marc) macrumors 6502a

    (marc)

    Joined:
    Sep 15, 2010
    Location:
    the woods
    #9
    You might get more help if you stopped making a fool of yourself.
     

Share This Page