zoukankan      html  css  js  c++  java
  • c++文件输入输出

          调用文件fstream头文件

          通过ofstream 标识符     进行定义//猜着好像c语言中的FILE *;

           文件写入类似于cout<<进行文本写入输出

    主要代码为

       char automonile[50];
        int year;
        double a, b;
        ofstream outfile;
        outfile.open("info.txt");
        cout << "输入文本";
        cin.getline(automonile,50);
        cin >> year;
        cin >> a;
        b = 0.94*a;
        outfile<< fixed;
        outfile.precision(2);
        outfile.setf(ios_base::showpoint);//是设置小数点模式就是说,设定为showpoint后,在不必要的时候也显示10进数的小数点以及其后的0
        outfile << automonile << endl;
        outfile << year << endl;
        outfile << a << endl;
        outfile << b << endl;
        outfile.close();

       进行文本的写入

       文本读取

          也是通过fstream文件中的ifstream 标识符;

       书中的文件名通过

       char filename[20];
        int count = 0;
        double sum = 0.0;
        double value;
        cin >> filename;
        ifstream infile;
        infile.open(filename);
        if (!infile.is_open())
        {
            cout << "open file" << filename << "failed"<<endl;
            exit(EXIT_FAILURE);
        }
        infile >> value;
        while (infile.good())
        {
            cout<< value<<" ";
            ++count;
            sum += value;
            infile >> value;
        }
        if (infile.eof())
            cout << "file is eof";
        if (count == 0)
            cout << "no processed";
        else {
            cout << count <<endl<< sum;
        }
        infile.close();

    文件写入完成

  • 相关阅读:
    数独小算法,测试通过(Java)
    OC运行时和方法机制笔记
    AlertView点击确定后再执行后面的代码
    对第三方库集成方式的分析
    当程序进入后台时执行长时间代码
    iOS开发之GCD使用总结
    缓存网络请求的结果
    防止 NSTimer retain 作为 target 的 self
    获取设备唯一码
    原生网络请求以及AFN网络请求/异步下载
  • 原文地址:https://www.cnblogs.com/qxhn/p/6080427.html
Copyright © 2011-2022 走看看