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();

    文件写入完成

  • 相关阅读:
    前进篇
    2014年12月14日记
    转载了两篇别人写的话语
    想好了,也决定了
    活着
    c#字典排序
    插值转向
    unity手游使用terrian注意事项
    委托delegate 泛型委托action<> 返回值泛型委托Func<> 匿名方法 lambda表达式 的理解
    推荐博客关于uniy
  • 原文地址:https://www.cnblogs.com/qxhn/p/6080427.html
Copyright © 2011-2022 走看看