Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.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++ ofstream未正确命名创建的文件_C++_Ofstream - Fatal编程技术网

C++ ofstream未正确命名创建的文件

C++ ofstream未正确命名创建的文件,c++,ofstream,C++,Ofstream,这确实创建了一个我想要的文件,但是它只调用文件名而不是输入名,并且没有类型。它应该接受“story.txt”之类的输入并创建一个文本文件 #include <iostream> #include <fstream> #include <string> using namespace std; int main() { string userInput; string endWrite = "** STOP **"; string fi

这确实创建了一个我想要的文件,但是它只调用文件名而不是输入名,并且没有类型。它应该接受“story.txt”之类的输入并创建一个文本文件

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
    string userInput;
    string endWrite = "** STOP **";
    string fileName = "";

    cin >> fileName;

    ofstream storyTime(fileName.c_str());

    storyTime.open("fileName");
    if (!storyTime.is_open()) {
        cout << "Could not open file " << fileName << "." << endl;
        return 1;
    }

    getline(cin, userInput);


    while (userInput != endWrite) {
        storyTime << userInput << endl;

        getline(cin, userInput);
    }
    storyTime.close();

        return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main(){
字符串用户输入;
字符串endWrite=“**停止**”;
字符串fileName=“”;
cin>>文件名;
流故事时间(fileName.c_str());
打开(“文件名”);
如果(!storyTime.is_open()){

无法
流故事时间(fileName.c_str());
应该已经打开了文件,所以
storyTime.open(“fileName”);
不是需要的。我猜您找错了文件的位置。@drescherjm噢,谢谢。如果流已经与文件关联,我将尝试删除打开状态的文档(即,它已经打开)调用此函数失败。因此,您第二次尝试打开名为“fileName”的文件将失败。@drescherjm hmmm它仍然只是命名文件名,而没有使用stream storyTime(fileName.c_str())的
类型;
应该已经打开文件,因此
storyTime.open(“fileName”);
不需要。我猜您在错误的位置查找文件。@drescherjm噢,谢谢。如果流已经与文件关联(即,它已经打开),调用此函数失败,我将尝试删除打开状态的文档。因此,您第二次尝试打开名为“fileName”的文件将失败。@drescherjm hmmm它仍然只是命名文件名而没有类型