Emuforums.com

Go Back   Emuforums.com > General Discussion > Web development / Programming
Home Register Downloads FAQ Members List Calendar Arcade Mark Forums Read


Reply
 
LinkBack Thread Tools Display Modes
Old October 14th, 2009   #1 (permalink)
Effulgent Plasma Chemist
 
TastEPlasma's Avatar
 
Join Date: Jul 2001
Posts: 271
Implementing a rudimentary save system

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. :-(
__________________
Computer Specs:

Hers: *c2q 9550 (2.83) @ 3.4Ghz *4GB OCZ Reaper DDR2-1066 5-5-5-15 *Sapphire Radeon 4870 w/ 1GB *SB Audigy SE *DFI LANParty DK X48-T2RS *700W OCZ GXS *320GB WD SATA2 *SBC DSL ISP *Samsung 906cw

His: *c2d 6320 (1.86) @ 3.3Ghz *2GB OCZ flex DDR2-800 4-4-5-15 @ 950 *BFG 8800GTX @ 632core, 2000mem *SB Audigy SE *eVGA 680i (122-CK-NF63-TR) *700W OCZ GXS *400GB WD sata2 *SBC DSL ISP *Samsung 226bw *Nautilus 500 water cooling system on gpu, cpu, and mem


Last edited by TastEPlasma; October 14th, 2009 at 00:16.. Reason: Screwed up thread title
TastEPlasma is offline   Reply With Quote

Advertisement [Remove Advertisement]
Old October 14th, 2009   #2 (permalink)
Dax
ライチュウ
 
Dax's Avatar
 
Join Date: Nov 2006
Location: USA
Posts: 3,289
Quote:
Originally Posted by TastEPlasma View Post
Greetings,

...
Can't you just use another string parameter in the function declaration, that would be your dynamic file? ...Or did I miss the point, here?
Dax is offline   Reply With Quote
Old October 14th, 2009   #3 (permalink)
Effulgent Plasma Chemist
 
TastEPlasma's Avatar
 
Join Date: Jul 2001
Posts: 271
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.
__________________
Computer Specs:

Hers: *c2q 9550 (2.83) @ 3.4Ghz *4GB OCZ Reaper DDR2-1066 5-5-5-15 *Sapphire Radeon 4870 w/ 1GB *SB Audigy SE *DFI LANParty DK X48-T2RS *700W OCZ GXS *320GB WD SATA2 *SBC DSL ISP *Samsung 906cw

His: *c2d 6320 (1.86) @ 3.3Ghz *2GB OCZ flex DDR2-800 4-4-5-15 @ 950 *BFG 8800GTX @ 632core, 2000mem *SB Audigy SE *eVGA 680i (122-CK-NF63-TR) *700W OCZ GXS *400GB WD sata2 *SBC DSL ISP *Samsung 226bw *Nautilus 500 water cooling system on gpu, cpu, and mem

TastEPlasma is offline   Reply With Quote
Old October 14th, 2009   #4 (permalink)
Dax
ライチュウ
 
Dax's Avatar
 
Join Date: Nov 2006
Location: USA
Posts: 3,289
Why not? Do you get an error? Or is it buggy?
Dax is offline   Reply With Quote
Old October 14th, 2009   #5 (permalink)
Effulgent Plasma Chemist
 
TastEPlasma's Avatar
 
Join Date: Jul 2001
Posts: 271
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 *'"
__________________
Computer Specs:

Hers: *c2q 9550 (2.83) @ 3.4Ghz *4GB OCZ Reaper DDR2-1066 5-5-5-15 *Sapphire Radeon 4870 w/ 1GB *SB Audigy SE *DFI LANParty DK X48-T2RS *700W OCZ GXS *320GB WD SATA2 *SBC DSL ISP *Samsung 906cw

His: *c2d 6320 (1.86) @ 3.3Ghz *2GB OCZ flex DDR2-800 4-4-5-15 @ 950 *BFG 8800GTX @ 632core, 2000mem *SB Audigy SE *eVGA 680i (122-CK-NF63-TR) *700W OCZ GXS *400GB WD sata2 *SBC DSL ISP *Samsung 226bw *Nautilus 500 water cooling system on gpu, cpu, and mem

TastEPlasma is offline   Reply With Quote
Old October 14th, 2009   #6 (permalink)
Global Moderator
 
cooliscool's Avatar
 
Join Date: Jul 2001
Location: South Carolina, USA
Posts: 6,824
Quote:
Originally Posted by TastEPlasma View Post
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.
cooliscool is online now   Reply With Quote
Old October 14th, 2009   #7 (permalink)
Effulgent Plasma Chemist
 
TastEPlasma's Avatar
 
Join Date: Jul 2001
Posts: 271
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
__________________
Computer Specs:

Hers: *c2q 9550 (2.83) @ 3.4Ghz *4GB OCZ Reaper DDR2-1066 5-5-5-15 *Sapphire Radeon 4870 w/ 1GB *SB Audigy SE *DFI LANParty DK X48-T2RS *700W OCZ GXS *320GB WD SATA2 *SBC DSL ISP *Samsung 906cw

His: *c2d 6320 (1.86) @ 3.3Ghz *2GB OCZ flex DDR2-800 4-4-5-15 @ 950 *BFG 8800GTX @ 632core, 2000mem *SB Audigy SE *eVGA 680i (122-CK-NF63-TR) *700W OCZ GXS *400GB WD sata2 *SBC DSL ISP *Samsung 226bw *Nautilus 500 water cooling system on gpu, cpu, and mem

TastEPlasma is offline   Reply With Quote
Old October 14th, 2009   #8 (permalink)
Global Moderator
 
cooliscool's Avatar
 
Join Date: Jul 2001
Location: South Carolina, USA
Posts: 6,824
Quote:
Originally Posted by TastEPlasma View Post
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.
cooliscool is online now   Reply With Quote
Old October 14th, 2009   #9 (permalink)
Effulgent Plasma Chemist
 
TastEPlasma's Avatar
 
Join Date: Jul 2001
Posts: 271
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?
__________________
Computer Specs:

Hers: *c2q 9550 (2.83) @ 3.4Ghz *4GB OCZ Reaper DDR2-1066 5-5-5-15 *Sapphire Radeon 4870 w/ 1GB *SB Audigy SE *DFI LANParty DK X48-T2RS *700W OCZ GXS *320GB WD SATA2 *SBC DSL ISP *Samsung 906cw

His: *c2d 6320 (1.86) @ 3.3Ghz *2GB OCZ flex DDR2-800 4-4-5-15 @ 950 *BFG 8800GTX @ 632core, 2000mem *SB Audigy SE *eVGA 680i (122-CK-NF63-TR) *700W OCZ GXS *400GB WD sata2 *SBC DSL ISP *Samsung 226bw *Nautilus 500 water cooling system on gpu, cpu, and mem

TastEPlasma is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 11:57.

© 2006 - 2008 Emu Forums | About Emu Forums | Legal | A member of the Crowdgather Forum Community


Powered by vBulletin® Version 3.7.6
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC5