简体   繁体   中英

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

basically i want to be able to load text files. This is fine. But i want to put the text files in a folder inside the project folder. Which is proving to be a problem because once the file is moved i cannot load it. I have tried entering the path but i just get a run-time. Can anybody help me with this.

Here is my code:

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<< "\n\t******Menu******\n\n";
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下,也可以使用一个正斜杠/代替。

The problem i had was that i didnt escape my slashes. In C++ the slash is read for things like \\n or \\t. So basically only having one \\ meant nothing to the compiler. I needed to do this: "Text File\\\\Introduction.txt"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM