首先,fopen()打开words.txt文件并读取文件内容。然后,fgets()从文件中读取一行并将起放入word中。fgets()调用会保留分开每一行的换行符,因为如果留下它,换行符会倍记为单词中的一个字符。
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// insert code here...
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 %d characters long", word, strlen(word));
}
fclose(wordFile);
return (0);
[pool drain];
return 0;
}
本文介绍了一个使用C语言进行文件操作的例子,演示了如何利用fopen()打开文件,以及如何利用fgets()逐行读取文件内容,并通过NSLog打印每行的长度。
187

被折叠的 条评论
为什么被折叠?



