Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从函数中打开整个代码的文件? 我正在编写一个简单的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); } } #包括 #包括 #包括 #包括 使用名称空间std; 字符串openFile(字符串文件名); 内部主(空) { 字符串文件名; cout文件名; openFile(文件名); 因为你的代码有很多缺陷_C++_File_Function_Input_Output - Fatal编程技术网

如何从函数中打开整个代码的文件? 我正在编写一个简单的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); } } #包括 #包括 #包括 #包括 使用名称空间std; 字符串openFile(字符串文件名); 内部主(空) { 字符串文件名; cout文件名; openFile(文件名); 因为你的代码有很多缺陷

如何从函数中打开整个代码的文件? 我正在编写一个简单的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); } } #包括 #包括 #包括 #包括 使用名称空间std; 字符串openFile(字符串文件名); 内部主(空) { 字符串文件名; cout文件名; openFile(文件名); 因为你的代码有很多缺陷,c++,file,function,input,output,C++,File,Function,Input,Output,fout#包括 #包括 #包括 #包括 使用名称空间std; 流式流量计; 字符串openFile(字符串文件名); void closeFile(); 内部主(空) { 字符串文件名; cout文件名; openFile(文件名); if(fout.good())//通过选中.good()在该文件中以任何方式使用fout 非常感谢你的代码,但我希望能够写入一个文件并从中读取。我如何修改此代码才能做到这一点?你希望写入一个文件并从同一个文件或不同的文件中读取。我希望它从用户输入的文件中读取,并输

fout
#包括
#包括
#包括
#包括
使用名称空间std;
流式流量计;
字符串openFile(字符串文件名);
void closeFile();
内部主(空)
{
字符串文件名;
cout文件名;
openFile(文件名);
if(fout.good())//通过选中.good()在该文件中以任何方式使用fout

非常感谢你的代码,但我希望能够写入一个文件并从中读取。我如何修改此代码才能做到这一点?你希望写入一个文件并从同一个文件或不同的文件中读取。我希望它从用户输入的文件中读取,并输出到名为“Output.txt”的文件中那你为什么要在func中打开文件并通过程序访问呢?因为我将使用一系列不同函数中的文件,并且希望保持main的干净。
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
ifstream fin(fileName.c_str());
    #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();
    }