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

    文件写入完成

  • 相关阅读:
    编程作业4.1:神经网络反向传播(BP算法)
    700. Search in a Binary Search Tree
    671. Second Minimum Node In a Binary Tree
    653. Two Sum IV
    606. Construct String from Binary Tree
    590. N-ary Tree Postorder Traversal
    589. N-ary Tree Preorder Traversal
    617. Merge Two Binary Trees
    SHELL 变量
    egrep 正则表达式
  • 原文地址:https://www.cnblogs.com/qxhn/p/6080427.html
Copyright © 2011-2022 走看看