在fgets中是否需要尾随换行符? [英] Is trailing newline necessary in fgets?

查看:122
本文介绍了在fgets中是否需要尾随换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用'fgets'和'newline'关键字进行搜索时,有很多关于如何删除尾随换行符的文章(这种删除似乎很麻烦).但是,似乎几乎没有什么解释可以包括fgets的换行符.同样在C ++中,"std :: getline"和"std :: istream:getline"方法将不保留换行符.那有什么理由吗?

解决方案

令人满意的(IMHO)说明:
http://www.cplusplus.com/reference/cstdio/fgets/

特别是:

换行符使fget停止读取,但是该函数将其视为有效字符并包含在复制到str的字符串中.

When I search using keywords of 'fgets' and 'newline', there are many posts regarding how to remove the trailing newline character (and such removal appears to be a burden). Yet it seems there is few explaination on how that newline is necessary for fgets to include. Also in C++, the 'std::getline' and 'std::istream:getline' methods will not keep the newline character. So is there a reason for it?

解决方案

Here is satisfying (IMHO) explanation:
http://www.cplusplus.com/reference/cstdio/fgets/

Especially:

A newline character makes fgets stop reading, but it is considered a valid character by the function and included in the string copied to str.

这篇关于在fgets中是否需要尾随换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆