验证fgets末尾自动添加的字符是'\0', 还是'\n'.
2016-03-14 12:22
363 查看
最近写代码经常使用字符串,对于输入函数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'.
相关文章推荐
- C-C++字符输出时遇到字符'\n','\0'的区别
- iOS中实现多线程的技术方案
- 程序员眼中的英语单词
- Listview的item和里面控件点击事件冲突
- 杨辉三角--JAVA代码实现
- springmvc学习笔记(18)-json数据交互
- Web中的安全性问题
- 我的进度条
- QT如何设置应用程序的图标
- Java包
- Java抽象类
- Java多态的应用
- Javafinal变量
- Javafinal方法
- Java之instanceof
- Java静态方法不能被覆盖
- Java编译时根据调用该方法的类或对象所属的类决定
- Java子类与父类方法的隐藏和覆盖
- 【图象处理】图文详解YUV420数据格式
- SMART原则