Separate settings file?

closed account (4Gb4jE8b)
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.
does 0 translate to true in bool statements?
no. false == 0, true != 0

why does it not even create the "Settings" file?
because it's 'ostringstream', not a file

not seem to check if the string is empty?
due to line 7 it won't be empty. Btw you can write setting_file.str().empty() to check if it's empty
closed account (4Gb4jE8b)
why does it not even create the "Settings" file?
because it's 'ostringstream', not a file


i was referring to
1
2
fstream settings;
settings.open("Settings", fstream::in | fstream::out);


but many thanks for your other answers. They did help
Last edited on
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
Last edited on
closed account (4Gb4jE8b)
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.
headlessgargoyle: well i need that file to be both in and out, without erasing it everytime... Is there anyway around the problem you mentioned?

Do a "test open" first to determine if the file exists. So something like this:

1
2
3
4
5
6
7
8
9
//Your code code code
settings.open("Settings", fstream::in);

if(settings.is_open)
{
   //Use a different file name, set fstream::app etc...
}

//code code code 
Last edited on
closed account (4Gb4jE8b)
I don't quite understand what you mean, can you explain a little more in depth computergeek?
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.
Last edited on
closed account (4Gb4jE8b)
Ahh okay i see what you're saying, but my problem was that settings.open() doesn't work how i want it too, and settings.is_open does return false.

Sadly I don't have a more specific question at this time because my mind is jumbled. Thank you for all of your guys' help. I really do appreciate it.
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.
Last edited on
closed account (4Gb4jE8b)
I think i finally figured out what you were saying. Might it have looked something like this?

1
2
3
4
5
6
7
8
9
10
        if(fexists("Settings") == false)
	{
		settings.open("Settings", fstream::out);
		settings.close();
		settings.open("Settings", fstream::out | fstream::in);
	}
	else
	{
		settings.open("Settings", fstream::out | fstream::in);
	}

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

all headers are included
Last edited on
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.
closed account (4Gb4jE8b)
oye vey, my apologies.

http://pastebin.com/Qsq6zJL5

that should do it. And i do understand why headers are necessary, and thank you very much for your help
headlessgargoyle wrote:
Might it have looked something like this?
almost:
1
2
3
4
5
6
7
8
    if(fexists("Settings"))
    {
        settings.open("Settings", fstream::out | fstream::in);
    }
    else
    {
        settings.open("Settings", fstream::out | fstream::in | fstream::trunc);
    }
Btw you don't need to write if(fexists("Settings") == true) nor if(fexists("Settings") == false) -> if(!fexists("Settings")) // note that '!'

headlessgargoyle wrote:
i then go on to check if the file has content by doing this
there's a simplier approach:
1
2
3
4
5
6
    while(settings.good())
    {
        char a = settings.get();
        if(!settings.eof())
                setting_file << a;
    }
it doesn't matter whether the file is empty or not you will reach the end of file
Last edited on
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 ?
sohguanh wrote:
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
Last edited on
closed account (4Gb4jE8b)
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.
Is the file opened correctly/at all (notice that fstream::trunc if not exists) then you might need to call flush() before closing the file
closed account (4Gb4jE8b)
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.
Topic archived. No new replies allowed.