【问题标题】:How do I open a file from a function for the whole code?如何从整个代码的函数中打开文件?
【发布时间】:2014-11-25 06:26:55
【问题描述】:

我正在编写一个简单的 c++ 脚本,并希望将打开文件的整个过程放在一个函数中。但是,当我尝试时,我的主要功能出现错误。谁能帮我?这是我的代码:

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

string openFile(string fileName);
int main(void)
{
    string fileName;
    cout << "Please input the file name (including the extension) for this code to read from." << endl;
    cin >> fileName;

    openFile(fileName);

    fout << "File has been opened" << endl;
    return 0;
}

string openFile(string fileName)
{
    ifstream fin(fileName);
    if (fin.good())
    {
        ofstream fout("Output");
        cout << fixed << setprecision(1);
        fout << fixed << setprecision(1);
        //Set the output to console and file to be to two decimal places and 
        //not in scientific notation
    }
    else 
    {
        exit(0);
    }
}

【问题讨论】:

    标签: c++ file function input output


    【解决方案1】:
        #include <iostream>
        #include <fstream>
        #include <string>
        #include <iomanip>
    
        using namespace std;
    
        ofstream fout;
        string openFile(string fileName);
        void closeFile();
    
        int main(void)
        {
            string fileName;
            cout << "Please input the file name (including the extension) for this code to read from." << endl;
            cin >> fileName;
    
            openFile(fileName);
            if (fout.good()) //use fout in any way in this file by cheking .good()
               cout << "File has been opened" << endl;
            closeFile();
           return 0;
        }
    
        string openFile(string fileName)
        {
            cout << fixed << setprecision(1);
            fout.open(fileName.c_str());
            if (fout.good()) {
               fout << fixed << setprecision(1);
               cout<<"Output file opened";
            }
        }
        void closeFile()
        {
           fout.close();
        }
    

    【讨论】:

    • 感谢您的代码,但我希望能够写入文件以及读取文件。我将如何修改它才能做到这一点?
    • 您是要写入文件并从同一文件或不同文件读取。
    • 我希望它从用户输入的文件中读取,并输出到名为“Output.txt”的文件中
    • 那为什么要在func中打开文件并通过程序访问?
    • 因为我要使用来自一堆不同函数的文件,并且想要保持 main 干净。
    【解决方案2】:

    你的代码有很多缺陷,

    1. fout &lt;&lt; "File has been opened" &lt;&lt; endl;,应该是,

      cout &lt;&lt; "File has been opened" &lt;&lt; endl;

    2. 您不能再次重新定义相同的变量。

      ofstream fout("Output");// first 
      cout << fixed << setprecision(1);
      fout << fixed << setprecision(1);
      //Set the output to console and file to be to two decimal places and 
      //not in scientific notation
      ofstream fout("Tax Output.txt");//second
      

    在最后一行给变量取一些其他的名字。

    1. 你正在传递std::string,你应该传递const char *

    ifstream fin(fileName);

    应该是,

    ifstream fin(fileName.c_str());
    

    【讨论】:

      猜你喜欢
      • 2016-05-26
      • 1970-01-01
      • 2018-09-18
      • 1970-01-01
      • 2021-02-18
      • 2015-12-20
      • 2021-03-20
      • 2014-01-06
      • 2015-07-03
      相关资源
      最近更新 更多