Writing string to text file

Discussion in 'Mac Programming' started by SpicyAl, Sep 13, 2011.

  1. SpicyAl macrumors newbie

    Joined:
    Sep 13, 2011
    #1
    Hey,
    I'm relatively new to cocoa. I'm looking for a way to simply bit of code to write a string to a text file and keep adding strings to the text file. I've tried a few different ways but keep getting problems. If you've got any ideas I'd really appreciate it.
    Thanks,
    a.
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    Post your code for what you've tried so far.

    Describe exactly what the problems are.
    Does the code fail to compile? If so, post the actual error messages.
    Does it compile but not work as expected? If so, describe what actually happens.

    Also explain where the strings come from that you want appended to the file. Are they hard-wired into the program? Entered by a user? Read from somewhere else?

    Rules of Thumb:
    1. Be specific.
    2. Post your code.
    3. Describe what you expected to happen.
    4. Describe what actually happened.


    The simplest way I can think of to append strings to a file doesn't use Cocoa. It just uses plain C standard I/O functions. Steps are:
    1. use fopen() to open a file in append mode ("a" or "a+").
    2. use fprintf() to print the strings to the opened file.
    3. use fclose() to close the file.

    You should read the man pages for those functions, or use Xcode's builtin help to get the reference docs, or consult a book on basic C I/O functions.
     
  3. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #3
    If you are going to stick to using Cocoa, you access files using NSFileHandle. File handles want to read or write NSData only, but fortunately, NSString has a couple of handy conversion methods to serve just that need. From the documentation, it looks to me like the data object does not include a \0 terminator for the string, which is consistent with the structure of text files. If you want to read the strings back as you wrote them, you will have to put in a delimiter (like \n) yourself, I imagine. You really only need to look at the docs for NSString and NSFileHandle, the NSData object will take care of itself.

    This should be at least as simple as the plain C option chown33 suggests.
     
  4. foidulus macrumors 6502a

    Joined:
    Jan 15, 2007
    #4
    The by far the simplest way to write a string to a file is to use NSStrings writeToFile method. However you cannot append to the file using that method, so you may need to look at what the people above have posted if you need anything more complicated.
     
  5. basher macrumors 6502

    basher

    Joined:
    May 27, 2011
    Location:
    Glendale, AZ USA
    #5
    You could use C functions like fopen() and fwrite() in your Obj-C code.

    If you wanted to stay purely Obj-C then I would probably say that collecting your strings via a NSMutable array and then writing the contents of the array to a file would be another worthwhile approach.

    Obj-C has a method to convert the NSMutableArray to a string. At the moment I'm too lazy to look up the make of the method.
     

Share This Page