I've been working on a means to load and save to a settings file. Here's what i've come up with so far generally speaking...
in int main() with headers correctly listed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
fstream settings;
ostringstream setting_file;
settings.open("Settings", fstream::in | fstream::out);
while(settings.good())
{
char a = settings.get();
setting_file << a;
}
cout << "settings are as follows '" << setting_file.str() << "'\n";
cout << settings.is_open() << "\n";
if(setting_file.str() == "")
{
//create new settings file text
}
else
{
//load variables
}
now i have a few questions from this. First of all, an easy one (i hope) does 0 translate to true in bool statements? Second of all, why does it not even create the "Settings" file? Third of all, and finally, why does
if(setting_file.str() == "")
not seem to check if the string is empty? Any and all help is much appreciated, and forgive me for the numerous noob posts.
ok, I see. 'settings' is the file not 'setting_file'.
the problem is 'in' because it requires an existing file otherwise it fails to open and you can't write to it. With 'in' you need 'trunc' in order to create an empty file, but it always destroys the content as well.
char a = settings.get(); this return EOS (-1) if the file is empty mind you
the problem is 'in' because it requires an existing file otherwise it fails to open and you can't write to it. With 'in' you need 'trunc' in order to create an empty file, but it always destroys the content as well.
well i need that file to be both in and out, without erasing it everytime... Is there anyway around the problem you mentioned?
char a = settings.get(); this return EOS (-1) if the file is empty mind you
will the ostringstream store EOS(-1) and if so, how can i make it so it doesn't?
Regardless it seems to be returning settings.good() == false do you have any idea why?
Thanks for your help coder, i do really appreciate it.
The idea of my untested code is that if settings.open works, then settings.is_open will return true, meaning that the file already exists and as coder777 points out will be over written if you specify fstream::trunc. So to avoid this the "comment" that I wrote has two ideas off the top of my head about how you could preserve the contents of that file instead of simply "failing out".
Did you have a more specific question?
EDIT: Somewhere between lines 5 and 7 of my psuedo code I should have written:
settings.close;
This is so that you can reuse "settings" without having to rewrite all of your code in the event that the file already exists.
Also another alternative might be if the file already exists just to read the whole thing into memory then spit it back out again. This is a sloppy but easy way to save the data.
Assuming that you're coming back to this thread; it is ok that settings.is_open returns false. You just create a new file with an else{...} statement, line 8 in my psuedo-code, and go from there.
where fexists returns a bool value of whether or not the file exists.
i then go on to check if the file has content by doing this
1 2 3 4 5 6 7 8 9 10 11 12
int e,s;
settings.seekg(0,ios::beg);
s = settings.tellg();
settings.seekg(0,ios::end);
e = settings.tellg();
settings.seekg(0,ios::beg);
while(s!=e && settings.good() == true)
{
char a = settings.get();
setting_file_content << a;
}
and then if s == e i write the file content. But i'm having a problem... the program accesses the if statement for writing the file but by doing settings << "some stuff here" it's not writing to the file. Any ideas?
here's a (more) complete copy of the code
http://pastebin.com/UzRhqH6m
Headers are NOT included in the sample code that you posted at pastebin. What header files are you using? This may not seem like it matters, but if I know what headers to look for the commands under I'll be able to help you faster and with definate answers.
Btw you don't need to write if(fexists("Settings") == true) nor if(fexists("Settings") == false) -> if(!fexists("Settings")) // note that !
Will there be any performance improvement between writing an explict test for bool true/false over !fexists(...) ?
I notice C++ presumably from C background like to use if (func()) do something than if (func()==true) do something.
In fact the explicit test for bool value make the code easier to understand unless there is significant performance improvement using the implicit test approach ?
In fact the explicit test for bool value make the code easier to understand unless there is significant performance improvement using the implicit test approach ?
I wouldn't say that there's a difference performance wise (the compiler generates likely the same code). So it's a matter of taste. But keep in mind that '=' vs '==' problem
thanks for your help guys, and i do like the conversation as it does teach me quite a bit. Though i personally agree with sohguanh, it makes the code easier to read, and as there is little to no performance difference (and it's a small program anyway), i'll keep the bool statement.
Also i've never personally fallen (and hopefully won't, at least for awhile) for the '=' vs '==' problem as it was made to be of critical importance when i first started learning c++.
Do you guys see any reason why settings << "some text here" isn't actually inputting to the file? Of all i've tried i've seen no change.
The file was opened correctly, and i don't know why it made a difference, but when i put all of the text into an ostringstream and passed that into the file it worked.