验证fgets末尾自动添加的字符是'\0' 还是 '\n\'?
2016-03-12 10:35
531 查看
最近写代码经常使用字符串,对于输入函数fgets网上有人说输入结束会在末尾自动添加’\n’,还有人说添加的是’\n’,我决定亲自验证:
#include "iostream" #include "stdio.h" #include "stdio_ext.h" #include "stdlib.h" #include "string.h" using namespace std; int main(int argc, char const *argv[]) { char buf[1024]; while(true){ __fpurge(stdin); int len = 0; fgets(buf,sizeof(buf),stdin); cout<<"buf="<<buf; len = strlen(buf); cout<<"valid size="<<len<<endl; cout<<"actual size="<<sizeof(buf)<<endl; if (buf[len] == '\0') { cout<<"is 0"<<endl; }else if (buf[len] == '\n') { cout<<"is n"<<endl; } } return 0; }
PC测试结果: iuc@iuc-linux ~/Project/LinuxTestCode $ ./fgets qqqq buf=qqqq valid size=5 actual size=1024 is 0 分析结果: fgets输入函数在末尾自动添加的'\0'而不是'\n'.
相关文章推荐
- 验证fgets末尾自动添加的字符是'\0' 还是 '\n\'?
- java NIO
- 开通博客的小感受
- 我的Android进阶之旅------>Android自定义View实现带数字的进度条
- 开源飞控
- LabF nfs window client
- 整除
- Android Material Design(4) CoordinatorLayout,AppBarLayout,Toolbar以及TabLayout的使用
- winmail.dat
- 设计一个算法,输出从u到v的全部最短路径(採用邻接表存储)
- 查看cache中消耗性能的语句
- Java String.split()的一些注意点
- 《软件工程》课之-调查问卷的心得体会
- 分布式锁的三种实现方式
- MySQL索引类型总结和使用技巧以及注意事项
- 沙盒机制
- github客户端命令行
- sqlserver2008 R2中查找未使用过的索引
- haneWIN NFS Server|haneWIN NFS Server(优秀NFS服务器)V1.2.16官方版-绿色下载吧
- 【转】如何新建索引:高开销的缺失索引