美文网首页C语言
C++格式化I/O

C++格式化I/O

作者: SilenceJiang | 来源:发表于2020-02-08 11:24 被阅读0次

cout格式化输出

  • 基本格式化操作

    • 进制转换

      //set radix(permanent change)
          int a=10;
          cout<<hex;
          cout<<a<<endl<<dec<<a;
      
    • 数字长度控制

      //set width(temporary change)
          int b=101;
          //function call
          cout.width(5);
          cout<<endl<<b<<endl<<b*b;   //write can be combined but not width
                                      //cout.width(5)<<endl<<b<<endl<<b*b is wrong
          //#include<iomanip>
          cout<<endl<<setw(15)<<b<<endl<<b*b;
          
      
    • 字符填充

      //fill blank(permanent change)
          int c=10101;
          //function call
          cout.fill('*');             //space default
          cout<<endl<<setw(20)<<c;
          cout.fill(' ');
          
          //#include<iomanip>
          cout<<endl<<setfill('*')<<setw(20)<<c;
      
    • 浮点数精度控制

      //float precision(permanent change)
          double d=3.1425;
          cout<<endl<<d;
          
          cout.precision(2);          //total witdth is 2
          cout<<endl<<d<<endl;
          
          cout<<endl<<fixed<<d;       //width after point
          
          //#include<iomanip>
          cout<<endl<<fixed<<showpos<<setprecision(6)<<d;
      
    • 字符串输出位数控制

      //only valid for char type string
          char *s="hello";
          cout.write(s,3);    //print hel
      
  • 注意事项

    • c++输出字符串指针地址

      //char array type
          char *s="hello";
          cout<<s;        //print hello
          cout<<(void *)s;//print an address in hex like 0x12345678
      
      //string type
          string s="hello";
          cout<<s;
          cout<<&s;
      
    • <iomanip>中的格式控制setw()|setprecision()|setfill()仅对数字有用,不能控制字符串的格式。

  • 常见标准控制符

    标识符 作用
    showbase 输出基数前缀
    showpoint 显示末尾小数点
    showpos 正数前显示+
    uppercase 16进制输出时字母大写 eg.15E
    left/right 居左/右显示
    dec/hex/oct 进制转换
    fixed 定点模式
    scientific 科学模式
    internal 基数前缀左对齐 右对齐

cin格式化输入

  • 基本格式化操作

    • >>抽取运算符

      //input 1 2 3 3 4h
      //output 13
          int num;
          int sum=0;
          while(cin>>num){
              sum+=num;
          }
          cout<<sum;
      
      //input silence and jiang
      //output silence
          char s[101];
          cin>>s;
          cout<<s;
      

      >>抽取运算符可以过滤所有的空白字符(回车\r 换行\n 空格 制表符\t)

      当遇到文件尾(eof)或非法输入时不会改变参数(num)的值,同时返回0/false。

      但是出现类似4h这样的非法输入时,会将4抽取为合法输入,h则被丢弃。(类似于单字符输入)

    • 单字符输入

          char c;
          //c is parameter
          cin.get(c);
          
          //c is return value
          c=cin.get();
          cout<<c;
      

      cin.get()不过滤空白符,可用于空白符的输入。

      get()参数是否为空的区别

      cin.get(c) c=cin.get()
      返回调用对象cin 返回int型的字符编码
      返回bool类型:true/false 返回EOF
    • 字符串输入

          char s[101];
          
          //'\n' is still in buffer
          cin.get(s,101);
          
          //'\n' is absorbed but deserted
          cin.getline(s,101);
          
          //DIY end character
          cin.get(s,101,'#'); 
          cin.getline(s,101,'#');
      
  • cin的特殊操作

    • 丢弃字符

      //input a 2<Enter> d
      //output d
          char c;
          cin.ignore(3,'2');
          cin>>c;
          cout<<c;
      

      cin.ignore()在过滤空白字符的基础上忽略3个字符,直到字符‘2’结束

    • 空白行(行尾有回车)的判断

          char s[101];
          //won't terminate on empty line
          while(cin>>s);  //cin.fail() will never be set
          
          //terminate on empty line
          while(cin.get(s,101));
          while(cin.getline(s,101)&&s[0]!='\0');
          
          cout<<s;
      

      设置failbit的条件

      cin.get(s) cin.getline(s)
      空行/文件尾 文件尾/读取长度大于s长度
    • 查看buffer中下一个字符(用于判断结束条件)

      //input das
      //output d
          char c,n;
          while((c=cin.peek())!='a')n=cin.get();
          cout<<n;
      

      输入流中的'a'仍然存在,peek()不会抽取'a'

    • 将字符放回缓冲区

      //input m
      //output m m
          char a,c;
          cin>>a;
          cin.putback(a);
          cin>>c;
          cout<<a<<' '<<c;
      
  • cin的流状态

    • eofbit

      到达文件尾时置为1

    • badbit

      文件读取错误(流被破坏)时置为1

    • failbit

      读取类型错误时置为1

    状态判断

    cin.fail() cin.eof() cin.bad()

相关文章

  • C++格式化I/O

    cout格式化输出 基本格式化操作进制转换//set radix(permanent change) int...

  • C++ 输入输出总结

    C++支持两种I/O,第一种是从C语言继承来的,一种是由C++定义的面向对象I/O系统。 从C继承来的I/O操作 ...

  • c++标准I/O总结

    之前我们总结了Unix系统I/O和Unix标准I/O,这次我们就顺便总结一下C++I/O库,主要内容都来自C++ ...

  • fmt包的使用

    fmt包的使用 功能:fmt包实现了类似C语言printf和scanf的格式化I/O。格式化动作('verb')源...

  • C++的输入输出流总结

    c++ I/O(不包括文件) http://www.cnblogs.com/chinazhangjie/archi...

  • Golang标准库——fmt

    fmt包实现了类似C语言printf和scanf的格式化I/O。格式化动作('verb')源自C语言但更简单。Pr...

  • fmt print

    格式化 Golang的fmt包中含有格式化I/O函数,类似于C语言中的printf和scanf。 函数描述fmt....

  • 06-输入输出函数-指趣学院

    Go语言fmt包实现了类似C语言printf和scanf的格式化I/O, 格式化动作源自C语言但更简单 输出函数 ...

  • mac Android studio 快捷键键位

    格式化代码 option+ctrl+i 新建方法 新建变量 option + enter 强制关闭无法运行应用 o...

  • 第一章-了解nodeJs

    Google公司使用C++语言开发的一种高性能JS引擎,特点有非阻塞型I/O,事件环。 非阻塞型I/O 传统单线程...

网友评论

    本文标题:C++格式化I/O

    本文链接:https://www.haomeiwen.com/subject/lwtixhtx.html