Read file contents and store in string

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

  1. mehdies
    Expand Collapse
    macrumors member

    Joined:
    Jun 10, 2012
    #1
    Could you explain me how read a txt file and store contents in string variable in coreFoundation?
     
  2. gnasher729
    Expand Collapse
    macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #2
    Why not in Cocoa?
     
  3. PatrickCocoa
    Expand Collapse
    macrumors 6502a

    Joined:
    Dec 2, 2008
    #3
    Core Foundation

    I don't understand the Core Foundation part of the question. But in Cocoa, it's:
    Code:
    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
    Expand Collapse
    macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #4
    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
    Expand Collapse
    thread starter macrumors member

    Joined:
    Jun 10, 2012
    #5
    I have a .cpp source code with I/O Kit and CoreFoundaton used in it.can i use Foundation?
     
  6. gnasher729
    Expand Collapse
    macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #6
    You can change it from .cpp to .mm and mix C++ and Objective-C.
     
  7. subsonix
    Expand Collapse
    macrumors 68040

    Joined:
    Feb 2, 2008
    #7
    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