当前位置: 移动技术网 > IT编程>开发语言>C/C++ > 终端对非打印字符的显示方式的有趣例子

终端对非打印字符的显示方式的有趣例子

2019年04月19日  | 移动技术网IT编程  | 我要评论

路由器wan口未连接,陈道学,superman 篮球火

有如下代码:

#include <iostream> 
#include <string> 
using namespace std; 
int main(int argc, char** argv) 

    char buffer[32] = {0}; 
    buffer[0] = 0; 
    buffer[1] = 'e'; 
    buffer[2] = 'l'; 
    buffer[3] = 'l'; 
    buffer[4] = 'o'; 
    string s(buffer, 32); 
 
    string b = "hello"; 
    b = s + b; 
    cout << "b:"<<b << endl; 
    cout <<"s.size():"<< s.size() << endl; 
    cout <<"s:"<< s << endl; 
    cout << "s.c_str()"<<s.c_str() << endl; 
 
    return 0; 

运行结果:
b:ellohello
s.size():32
s:ello
s.c_str()

重定向标准输出至一个文件(./a.out > a.txt),用vim打开a.txt,文件内容显示如下:
  1 b:^@ello^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@hello        
  2 s.size():32
  3 s:^@ello^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
  4 s.c_str()

 

摘自 积累浅知识的小菜一个--stay hungry, stay foolish

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网