• C++ 系统标准的输入输出流


    1.流的概念和流类库的结构(了解)

    标准IO:对系统的标准输入输出设备进行读写

    文件IO:对磁盘进行输入输出读写

    串IO:对内存进行读写

    2.成员函数

    cin.get() //一次只能读取一个字符

    cin.get(一个参数) //读一个字符

    cin.get(两个参数) //可以读字符串

    cin.getline()//取一行,换行符丢弃

    cin.ignore()//忽略

    cin.peek()//偷窥

    cin.putback()//放回

    cin.clear()

    cin.sync()

    cin.fail()

    1. #include <iostream>
    2. #include <iomanip>
    3. using namespace std;
    4. //判断用户输入的是字符串还是数字
    5. void test06()
    6. {
    7. cout << "请输入一个字符串或数字" << endl;
    8. char c=cin.peek();
    9. if (c >= '0'&&c <= '9')
    10. {
    11. int num;
    12. cin >> num;
    13. cout << "输入的数字是:" << num << endl;
    14. }
    15. else
    16. {
    17. char buf[1024] = { 0 };
    18. cin >> buf;
    19. cout << "输入的字符串是:" <<buf << endl;
    20. }
    21. }
    22. //输入一个010的数字,直到输入正确为止
    23. void test07()
    24. {
    25. int num;
    26. while (1)
    27. {
    28. cin >> num;
    29. if (num >= 0 && num <= 10)
    30. {
    31. cout << "输入正确" << endl;
    32. break;
    33. }
    34. cout << "重新输入:" << endl;
    35. //重置标志位
    36. cin.clear();
    37. //清空缓冲区
    38. //cin.sync();
    39. //2015
    40. char buf[1024] = { 0 };
    41. cin.getline(buf, 1024);
    42. //打印标志位
    43. cout << cin.fail() << endl;
    44. }
    45. }

    系统标准的输出流

    1.通过成员函数格式化输出

    1. //通过流成员函数实现格式化的输出
    2. void test03()
    3. {
    4. int num = 99;
    5. cout.width(20);//设置宽度
    6. cout.fill('*');//填充
    7. cout.setf(ios::left);//让数据在左边
    8. cout.unsetf(ios::dec);//卸载十进制
    9. cout.setf(ios::hex);//安装十六进制
    10. cout.setf(ios::showbase);//显示基数
    11. cout.unsetf(ios::hex);//卸载十六进制
    12. cout.setf(ios::oct);//安装八进制
    13. cout << num << endl;
    14. }

    2.通过控制符格式化输出  

    1. //通过控制符来格式化输出,引入头文件iomanip
    2. void test04()
    3. {
    4. int num = 99;
    5. cout << setw(20);//设置宽度
    6. cout << setfill('~');//填充
    7. cout << setiosflags(ios::showbase);//显示基数
    8. cout << setiosflags(ios::left);//让数据在左边
    9. cout << hex;//六十进制
    10. cout << oct;//八进制
    11. cout << dec;//十进制
    12. cout << num << endl;
    13. }

    3.打印浮点数后面的小数点

    1. void test05()
    2. {
    3. double d = 20.22;
    4. cout << setiosflags(ios::fixed);//设置显示浮点数
    5. cout << setprecision(10);//显示小数点后10
    6. cout << d << endl;
    7. }

  • 相关阅读:
    java计算机毕业设计贵州农产品交易系统MyBatis+系统+LW文档+源码+调试部署
    【LeetCode刷题-字符串】--6.N字形变换
    Python基础学习016__UnitTest
    二十四节气之立秋
    firewall 命令简单操作
    传输层详解
    格拉姆角场GAF将时序数据转换为图像并应用于凯斯西楚大学轴承故障诊断(Python代码,CNN模型)
    Redis 为什么这么快,你知道 I/O 多路复用吗?
    scala基础入门
    第24章_瑞萨MCU零基础入门系列教程之内部温度传感器-TSN
  • 原文地址:https://blog.csdn.net/qq_35496811/article/details/126754180