开发者

File parsing: C++ to cocoa

开发者 https://www.devze.com 2023-02-17 04:51 出处:网络
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

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?

0

精彩评论

暂无评论...
验证码 换一张
取 消