PDA

View Full Version : Read file contents and store in string




mehdies
Apr 29, 2013, 10:07 AM
Could you explain me how read a txt file and store contents in string variable in coreFoundation?



gnasher729
Apr 29, 2013, 10:29 AM
Could you explain me how read a txt file and store contents in string variable in coreFoundation?

Why not in Cocoa?

PatrickCocoa
Apr 29, 2013, 10:40 AM
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.

ArtOfWarfare
Apr 29, 2013, 02:37 PM
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.

mehdies
Apr 29, 2013, 11:59 PM
Why not in Cocoa?

I have a .cpp source code with I/O Kit and CoreFoundaton used in it.can i use Foundation?

gnasher729
Apr 30, 2013, 06:59 AM
I have a .cpp source code with I/O Kit and CoreFoundaton used in it.can i use Foundation?

You can change it from .cpp to .mm and mix C++ and Objective-C.

subsonix
Apr 30, 2013, 09:34 AM
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.