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

来源:岁月联盟 编辑:猪蛋儿 时间:2012-01-10

有如下代码:

#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

图片内容