zoukankan      html  css  js  c++  java
  • c++入门之输出文件流ofstream

     1 # include "iostream"
     2 # include"fstream"
     3 int main()
     4 {
     5     using namespace std;
     6 
     7     char automobile[50];
     8     int year;
     9     double a_price;
    10     double d_price;
    11 
    12     ofstream outfile;
    13     outfile.open("C:/Users/kb409/Desktop/carinfo.txt");//注意,这里使用"/"而不是""
    14     //outfile.open("carinfo.txt");
    15     cout << "Enter the make and model of automobile:";
    16     cin.getline(automobile, 50);
    17     cout << "Enter the model year:";
    18     cin >> year;
    19     cout << "Enter the original asking price:";
    20     cin >> a_price;
    21     d_price = 0.913*a_price;
    22 
    23     cout << fixed;//cout<<fixed是以一般格式而不是科学计数法输出浮点数
    24     cout.precision(4);//控制输出流显示浮点数的数字个数
    25     cout.setf(ios_base::showpoint);//显示最后一位小数后面的0;
    26     cout << "Make and model:" << automobile << endl;
    27     cout << "Year:" << year << endl;
    28     cout << "Was asking 's" << a_price << endl;
    29     cout << "Now asking 's" << d_price << endl;
    30     ////////////////////////////////
    31     outfile << fixed;//fixed未被定义,那么这里fix是什么?
    32     outfile.precision(2);
    33     outfile.setf(ios_base::showpoint);
    34     outfile << "Make and model:" << automobile << endl;
    35     outfile << "Year:" << year << endl;
    36     outfile << "Was asking 's" << a_price << endl;
    37     outfile << "Now asking 's" << d_price << endl;
    38     outfile.close();
    39     system("pause");
    40     return 0; 
    41 }

    该段代码的功能是:通过cout输出字符到显示端,同样的通过ofstream对象输出字符到文件端.。关于端输出流ostream和文件输出流fstream的一些本质性描述如下:

    上述<<c++ prime plus>>中描述了控制台输出和文件输出的基本流程。从其中可以看出,控制台输出和文件输出本质上差别并不大,只是对象不同而已,.

    针对上面给出的代码,总结要点:

    1 第13行代码outfile.open("C:/Users/kb409/Desktop/carinfo.txt");//注意,这里使用"/"而不是""  ,虽然桌面文件采用的“”,但很明显c++支持的是"/",否则无法生成文件

    2 关于23到25的代码,只是对数据格式的一个处理,见如下:

    (给出该知识点博客地址:https://blog.csdn.net/septemberAs/article/details/68489554)

    以上属于知识点型,不需要记忆,但要注意总结的第一条.

    以及需要认识到的是.控制台输出和文件输出至少从这里看是遵循相同的规则的

  • 相关阅读:
    elasticHD
    python3--socketIO_client 摸索怕坑指南
    治疗重新造轮子症系列——递归查询json数据中所有的key值
    REST FRAME WORK--认证 权限 Xadmin
    REST FRAME WORK--视图基类 视图集
    REST FRAME WOEK 请求与响应&视图
    REST FRAME WORK--1
    VUE--axios--对象提供的属性功能
    VUE--组件化开发--VUE-CLI
    Vue中发送ajax请求——axios使用详解
  • 原文地址:https://www.cnblogs.com/shaonianpi/p/9783251.html
Copyright © 2011-2022 走看看