zoukankan      html  css  js  c++  java
  • C++读写文件

    1、设置浮点数的显示精度

    //设置浮点数输出的小数位数 设置4位小数输出
    //方式1 
    cout.setf(ios_base::fixed,ios::floatfield);
    cout.precision(4);
    
    //方式2
    //使用控制符 要包含iomanip头文件
    cout << std::fixed << std::setprecision(4);
    

    2、C++ IO流简单读写文件

    double b = 123451.45;
    //写文本文件
    string file_path("d:\TEST\a.txt");
    ofstream fo;
    fo.open(file_path,ios_base::out | ios_base::trunc);
    if (!fo.is_open())
    {
        cout << "file open failed!" << endl;
        return;
    }
    fo << std::fixed;
    fo << std::setprecision(4) << b;
    fo << endl;
    fo << std::setprecision(2) << b;
    fo.close();
    
    //读文件
    cout << "Read File
    ";
    ifstream fi;
    fi.open(file_path,ios_base::in);
    if (!fi.is_open())
    {
        cout << "file can not open!" << endl;
        return;
    }
    string str;
    while(!fi.eof())
    {
        std::getline(fi,str);
        cout << str << endl;
    }
    fi.close();
    

      

  • 相关阅读:
    452.用最少数量的箭引爆气球
    134.加油站
    Javascript
    spring-JDBC模板
    AOP注解方式ApsectJ开发
    AOP通知类型
    AOP的使用
    AOP相关术语
    AOP
    IOC注解详解
  • 原文地址:https://www.cnblogs.com/cmranger/p/4373485.html
Copyright © 2011-2022 走看看