Read file contents and store in string

Discussion in 'Mac Programming' started by mehdies, Apr 29, 2013.

  1. mehdies macrumors member

    Jun 10, 2012
    Could you explain me how read a txt file and store contents in string variable in coreFoundation?
  2. gnasher729 macrumors P6


    Nov 25, 2005
    Why not in Cocoa?
  3. PatrickCocoa macrumors 6502a

    Dec 2, 2008
    Core Foundation

    I don't understand the Core Foundation part of the question. But in Cocoa, it's:
    NSString *myWonderfulString = [NSString stringWithContentsOfFile:[NSBundle mainBundle] pathForResource:@"MyAwesomeFile" ofType:@"plist"]];
    if your file is a plist that's part of the application's bundle.
  4. ArtOfWarfare macrumors G3


    Nov 26, 2007
    CFStringRef and NSString* have a "toll free bridge" meaning changing from one to the other involves nothing more than a cast.

    Just use the method PatrickCocoa suggested above and cast to a CFStringRef to store the contents in Core Foundation.
  5. mehdies thread starter macrumors member

    Jun 10, 2012
    I have a .cpp source code with I/O Kit and CoreFoundaton used in it.can i use Foundation?
  6. gnasher729 macrumors P6


    Nov 25, 2005
    You can change it from .cpp to .mm and mix C++ and Objective-C.
  7. subsonix macrumors 68040

    Feb 2, 2008
    Note that it can be done with CoreFoundation, it's just more work and more error prone. Have you looked at Apple's CFString reference? If so what exactly is it you are having problems with, there are a handful of functions for converting a buffer to a CFString depending on encoding and other things.

Share This Page