记下来免得自己忘了。
char *fgets(
char
*str,
int
n,
FILE
*stream
);
The fgets function reads
a string from the input stream argument and stores it
in str. fgets reads characters from the
current stream position to and including the first newline
character, to the end of the stream, or until the number of
characters read is equal to n – 1, whichever comes
first. The result stored in str is appended with a
null character. The newline character, if read, is included in the
string.
fgets从流的当前位置读取字符存到str中,碰到行尾、文件尾或者读到了n-1个字符就停止读取,并在存储的字符串末尾加上null字符作为字符串的结尾(读n-1个字符,加上null字符正好n个)。如果读到了换行符,也会将其存入str中。
对于换行符,msdn介绍fread时说了 :If the given
stream is opened in text mode, carriage return–linefeed pairs are
replaced with single linefeed characters. The replacement has no
effect on the file pointer or the return value.
如果是以文本方式打开的文件流,那么读入到str中的回车换行对(\r\n)会被替换为一个换行字符(\n)。可是,文件流本身中的\r\n并没有被替换成\n,因此这种替换并不会影响文件指针的位置,也不影响返回值。也就是说,在处理指针偏移量的时候,还是要把回车换行的长度记为2,而不是1。