失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > c语言换行符的作用 C语言读取文件一行以及换行符的问题

c语言换行符的作用 C语言读取文件一行以及换行符的问题

时间:2023-05-22 19:57:34

相关推荐

c语言换行符的作用 C语言读取文件一行以及换行符的问题

记下来免得自己忘了。

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。

如果觉得《c语言换行符的作用 C语言读取文件一行以及换行符的问题》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。