精通ObjectiveC系列 1) 文件读取

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    FILE *wordFile = fopen ("/tmp/words.txt", "r");
    char word[100];
    
    while (fgets(word, 100, wordFile))
    {
        // strip off the trailing \n
        word[strlen(word) - 1] = '\0';
        NSLog (@"%s is %lu characters long", word, strlen(word));
    }
    
    fclose (wordFile);
    return (0);
}

 文件内容:

Joe-Bob "Handyman" Brown
Jacksonville "Sly" Murphy
Shinara Bain
George "Guitar" Books

运行结果:

Joe-Bob "Handyman" Brown is 24 characters long
Jacksonville "Sly" Murphy is 25 characters long
Shinara Bain is 12 characters long
George "Guitar" Books is 21 characters long

技术改变世界
原文地址:https://www.cnblogs.com/davidgu/p/2918144.html