-
Save system...
Ok, This is driving me nuts, and searching google just leads me to dead ends so last resort to ask. I am try to create a file save system, but where the user can specify the name of the file. is this possible using ofstream? this wont work but an example of what I am trying to do
Code:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
string save = "data";
ofstream out ( + data +".txt" );
cin.get();
}
So is there a way to do something like that?
Thank you SO much
-
Correct.
std::getline()
Kuphryn
-
umm how would getline() do anything?
I am trying to make file names editable
EDIT:
if getline(0 would work can you show me how please?
-
MFC:
Code:
...
CFileDialog *dlg=new CFileDialog(true,
L"*.dat",
NULL,
OFN_FILEMUSTEXIST,
L"Data files (*.dat)|*.dat|"
L"All files (*.*)|*.*|");
INT_PTR result=dlg->DoModal();
if (result==IDOK)
{
CFile file(dlg->GetPathName(),CFile::modeRead);
CArchive Ar(&file,CArchive::load);
Serialize(Ar);
}
delete dlg;
...
-
Doesnt MFC require Microsoft Visual Studio? and will I not have to write my entire code in either MFC or win32 for that to work? Right now I am using a console, just because I cant use string in win32 with making the stupid char arrays, and atoi on all my integers to get them to go on screen. Console is easier because of support for std::string, but if I must go to win32 or MFC I can.. but if MFC requires MSVS then I cant I dont have a supported operating system
-
The question really belongs to c++ programming then.
Use c_str() in the ofstream declaration
Code:
#include <string>
#include <fstream>
#include <iostream>
int main()
{
std::string fileName;
std::cout << "Enter file name: ";
std::getline(std::cin, fileName);
//add extension to fileName
//however, should test first if the user already typed one
std::ofstream FileOut(fileName.c_str());
if (FileOut) {....
-
that is what kuphryn was suggesting, but I had no Idea how it worked. Thanks a million, and bubba you answer would be great if I was using MFC
EDIT: I post in windows, because I use alot of non-standard code, so by posting here non-standard doesnt matter.
-
Code:
#include <iostream>
#include <fstream>
int main()
{
using namespace std;
char ToSave[] = "Hello world!";
char Filename[32];
cout << "Specify filename: ";
cin >> Filename;
ofstream FileStream(strcat(Filename, ".txt"), ios::out | ios::trunc);
FileStream << ToSave;
cout << "Done.\n";
return 0;
}
-
Sorry if this is bumping the thread, but I figured this is better than a new thread.
I was wandering what the best way to create a navigatable enemy system?
so far I have just been using something like
Code:
struct enemy {
int hp[100];
int mp[100];
// and so on for the the stats.
};
enemy enemy1;
enemy enemy2;
enemy enemy3;
//one instance for each enemy in battle
of course this works fine until I try to have the weaker enemies not show up after the character reaches a certain level, because I was just using int x = rand();
I can tell it how far not to go, but I cant seem to tell it to skip a certain amount, and was wondering if there is a better approach to this? ( and I am also having trouble saving these values to a file, and then loading them ) Thank you
-
Last time I played games, we didn't need a 100 int array to store their HP... the times have changed I see.
You will probaly want to start looking into binary saving/reading, it isn't as hard as it sounds.
-
lol, well I have a feeling I am really doing it the hard way huh? I guess Ill look into binary stuff, but I have never been very good at binary
-
Once you get used to it, it is will seem like the logical choice for stuff like this. What you may want to do is make a reader into your game... then a character/enemy editor that is external. Same goes with maps.
-
hmm can you link me to some good references or tutorials? ( I am searching google right now ) but would like some help searching if you dont mind thanks alot
-
I learned what I know of binary saving/reading from decoding the diablo 2 save files and playing around with it, so I don't know any guides, sorry.
-