已知在tmp路径下有一个words.txt     (tmp/words.txt)文本内容:
Joe-Bob "Handyman" Brown
Jacksonville "Sly" Murphy
Shinara Bain
George "Guitar" BooksJoe-Bob "Handyman" Brown建立一个程序,其主函数体是:
FILE *wordFile=fopen("tmp/words.txt","r");
char word[100];
while(fgets(word,100,wordFile)){
word[strlen(word)-1]='0';
NSlog(@"%s is %d characters long",word,strlen(word));运行的结果是:
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===============>我的疑问是:while(fgets(word,100,wordFile))里的fget(word,100,wordFile),作用不是直接读取全部的99个字符吗?但按照结果,原理却好像是fget函数按一行一行地读取?这是怎么解释呢?

解决方案 »

  1.   

    char * fgets ( char * str, int num, FILE * stream );
    Get string from streamReads characters from stream and stores them as a C string into str until (num-1) characters have been read or either a newline or a the End-of-File is reached, whichever comes first.
    A newline character makes fgets stop reading, but it is considered a valid character and therefore it is included in the string copied to str.
    A null character is automatically appended in str after the characters read to signal the end of the C string.Parameters
    str
    Pointer to an array of chars where the string read is stored.
    num
    Maximum number of characters to be read (including the final null-character). Usually, the length of the array passed as str is used.
    stream
    Pointer to a FILE object that identifies the stream where characters are read from.
    To read from the standard input, stdin can be used for this parameter.