File parsing: C++ to cocoa
Can anyone help me to convert this开发者_运维百科 c++ function to cocoa? Here I am parsing file line by line. I need to know efficient NS function
std::ifstream Stream;
Stream.open(FilePath,std::ios_base::in);
if (Stream.is_open())
{
std::string Line;
std::string Read, Key, Value;
std::size_t i;
while( !Stream.eof() ) {
std::stringstream LineStream;
std::getline( Stream, Line );
printf("%s\n",Line);
}
}
approximately:
NSString * file = [[NSString alloc] initWithContentsOfFile:path]; // or url
NSCharacterSet * newlineSet = [NSCharacterSet newlineCharacterSet];
NSArray * lines = [file componentsSeparatedByCharactersInSet:newlineSet];
for (NSString * at in lines) {
printf("%s\n", [at UTF8String]);
}
[file release], file = 0;
but yes, like Goz mentioned: if it already works using c++, why convert it to objc?
精彩评论