文件 I/O.无法使用文件的路径

File I/O. Not able to use path of the file

本文关键字:文件 路径      更新时间:2023-10-16

基本上我希望能够加载文本文件。 这很好。 但是我想将文本文件放在项目文件夹内的文件夹中。 这被证明是一个问题,因为一旦文件被移动,我就无法加载它。 我尝试输入路径,但我只是得到了一个运行时。 任何人都可以帮我解决这个问题。

这是我的代码:

Storyline::Storyline()
{
    mStory = "Default";
}
Storyline :: Storyline(string story)
{
mStory= story;
}
void Storyline::print()
{
cout<< "Story= " << mStory<< endl;
cout<< endl;
}
void Storyline::save(ofstream& outFile)
{
outFile<< "Story = " << mStory<< endl;
outFile<< endl;
}
void Storyline::load(ifstream& inFile)
{
string garbage;
inFile>> garbage >> mStory;
}

void Storyline::Menu()
{
cout<< "nt******Menu******nn";
cout<< "Please choose one of the following:n";
cout<< "1 -Play Game.n";
cout<< "2 -Exit.n";
}
void Storyline::Story(string story)
{
ifstream Story;                                                                                                     
        string line;                                                                                                            
        Story.open(story);                                                                                        
        if (Story.is_open())                                                                                
        {
                while ( Story.good() )                                                                              
                {
                        getline (Story,line);                                                                       
                        cout << line << endl;
                }
                Story.close();                                                                                           
        }
        else{
                throw("GAME FILE LOADING FAILED!");                                                             
        }
}

如果你想使用双斜杠 \\ 来省去所有的麻烦,你可以使用一个正斜杠/代替,即使在 Windows 下也是如此。

我遇到的问题是我没有逃脱我的斜杠。 在C++中,对于 或 \t 之类的内容,将读取斜杠。 所以基本上只有一个\对编译器没有任何意义。 我需要这样做:"文本文件\\简介.txt"