Next Generation Emulation banner
1 - 9 of 9 Posts

· The Nexus of a Crisis, and The Origin of Storms
Joined
·
965 Posts
Discussion Starter · #1 · (Edited)
Greetings,

So I have been messing around again with c++ after my life settled the hell down.

I am writing a text based adventure game (yes, really ;)), and I want to store the text is seperate notepad files for easy reference and editing.

Here is what I have:
Code:
void story (string stf)
{
    string line;
    string stftxt = ".txt";
    stftxt.insert(0,stf);
    ifstream storyfile;
    storyfile.open (stftxt);
    if (storyfile.is_open())
    {
        while (! storyfile.eof() )
        {
            getline (storyfile, line);
            cout << line << endl;
        }
        storyfile.close();
    }
    else cout << "Unable to open file";
}
What I cant figure out is how to open a file based on the number I pass to the function via string stf. Originally I was trying to convert int stf into a string and insert it into stftxt, until I realized that I had no good reason to leave it an integer and not make it a string.

Anyways, how do I dynamically open different files depending on a value passed to the function "story"? Any ideas would be most helpful.

Thanks,
TastEPlasma

EDIT: Aww hell, I originally started to type this about a rudimentary save system, but I finally figured all that out (how to read/write arrays to a .txt file). Cant change the threads title now though. :-(
 

· The Nexus of a Crisis, and The Origin of Storms
Joined
·
965 Posts
Discussion Starter · #3 ·
Unfortunately not. The primary problem is that I cant use a variable with the function file.open.

i.e. This is valid:
Code:
 storyfile.open ("story.txt");
This is not:
Code:
 storyfile.open (story);
Regardless of the variable type that story is.
 

· The Nexus of a Crisis, and The Origin of Storms
Joined
·
965 Posts
Discussion Starter · #5 ·
I do get an error, it refuses to compile. I think it is a syntax error.

"cannot convert parameter 1 from 'std::string' to 'const wchar_t *'"
 

· Registered
Joined
·
7,407 Posts
I do get an error, it refuses to compile. I think it is a syntax error.

"cannot convert parameter 1 from 'std::string' to 'const wchar_t *'"
Try this:

Code:
storyfile.open (story.c_str());
string.c_str will convert the string to a null terminated array of chars, with the return value being a pointer to this array. Should be what you need.
 

· The Nexus of a Crisis, and The Origin of Storms
Joined
·
965 Posts
Discussion Starter · #7 ·
Absolutely awesome.

c_str - C++ Reference

Link for info.

Thank you so much, this has been plaguing me for a couple of hours now.

It works now. :-DDD
 

· Registered
Joined
·
7,407 Posts
Absolutely awesome.

c_str - C++ Reference

Link for info.

Thank you so much, this has been plaguing me for a couple of hours now.

It works now. :-DDD
No problem. Looking forward to your future progress. :)

Oh, and good to see you around after all these years. Not many of us original members left. ;)
 

· The Nexus of a Crisis, and The Origin of Storms
Joined
·
965 Posts
Discussion Starter · #9 ·
My aim is primarily just to familiarize myself with the data management techniques of OO programming (which, so far, I am loving). I have several progressively grander projects in mind, but I got a lot of basic knowledge to grasp first.

I am thinking about making this project modular, since I am halfway there anyway, so if someone wanted they could just write the text files and a single text-based control file that would give the events a flowchart. I figure this is good practice since I am continuing to subdivide the data and function into successively smaller, cleaner pieces.

I also want my future projects to be modular as well, so its good practice in that sense too.

As for the older members, there is a certain feeling of warmth and nostalgia to see people still kickin around after all these years, eh? :)
 
1 - 9 of 9 Posts
This is an older thread, you may not receive a response, and could be reviving an old thread. Please consider creating a new thread.
Top