Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

bmsfx

Stream + buffer question

This topic is 5399 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

okay, so first, i got this code aight.
	int readconfig()
	{
	ifstream hfile("config.ini");

		// timelog

	 	theTime1 = CTime::GetCurrentTime();
		CString d = theTime1.Format( "%H:%M:%S : " );
	
			// error control

			if (hfile == NULL) 
			{
			m_msgData.AddString(d +"Error => Config file could not be opened.");
			UpdateData(TRUE);
			errorlog("ERROR : Config file could not be opened.");
			
			return 0;
			}
			else
			{

			m_msgData.AddString(d +"Read config file with success.");
			UpdateData(TRUE);
			log("Read config file with success.");

			const int sz = 100; // Buffer size;

			char buf[sz];

			while(hfile.getline(buf, sz))
			{
			char* cp = buf;

			while(*cp != '=')
			cp++;
			cp += 1; // Past "="


			log(buf);
			m_msgData.AddString(d +buf);
			}
			
		return 0;
		}
	return 0;
	}
okay so this opens the file, reads each line until a break (\n) and then pufs it in a buffer. the cp is every line after =[this text here goes in cp] my config.ini file
Logging=0
MaxConnections=100
MinGameVer=01
okay so cp will be 0 100 01 (testet and it is like that) now to the questions, lets say i want to update a editbox with e.g logging on or off lets call it m_msgEditBox1 so if Logging=1 it should put that to ON and if its a 0, then it should put that to a OFF. I know how to put the text into the edit box, but how to i check if the Logging line is a 1 or a 0 ? since all is in the same buffer ? can you do like a read buffer[1] for line 1 and so ? or how do you do it ? any help would be appriciatet. [edited by - Bmsfx on February 26, 2004 1:45:12 PM]

Share this post


Link to post
Share on other sites
Advertisement
Assuming you have a bool called 'logging', when cp has passed the '=' you could do something like:

logging = *cp == '1';

(if the character at cp is '1', then logging is true; false otherwise.)

[edited by - Blew on February 26, 2004 11:39:05 PM]

Share this post


Link to post
Share on other sites
first, your check if the open failed is wrong. there's no NULL or anything, it's just

if(hfile) {
// Success
} else {
// failure
}

Next, you have to figure out a format to store the data in memory.

I suggest using an std::map with a data file that looks like that.

you would read your file like so: (this is untested "pseudo" code, so someone will need to correct errors for me...)

ifstream hfile(filename);
map<char*,string> myMap; // EDIT: stupid forums and lt/gt's
string key,value;
while(getline(hfile,key,'=')) {
// Reject blank lines
// Reject any comments you might have in the config file
if(getline(hfile,value))
myMap[key.c_str()] = value;
else
break; // EOF after the =
}

then you can get the values by doing something like

if(myMap["Logging"]="0") { ... }

etc.

[edited by - C-Junkie on February 27, 2004 5:07:35 PM]

Share this post


Link to post
Share on other sites
first, your check if the open failed is wrong. there's no NULL or anything, it's just

if(hfile) {
// Success
} else {
// failure
}

without the == NULL it will say the file is found even if its not there.

your idea sounds good, lets see if i can get it to work.

edit:

cant get the map stuff to work, there is some code missing.
and it gives quite alot of erros here and there.

btw i do have using namespace std;

your idea sounded VERY good though, since i could really use that.

[edited by - Bmsfx on February 27, 2004 12:23:12 PM]

Share this post


Link to post
Share on other sites
Blew your idea didnt give errors, but it didnt detect true or false, it always only returned true (or was it false) didnt matter if it was a 1 or 0 or 10 standing in the config files.

i had a:

bool Logging;

and the code u said put in at the right place.

then i tryed a (cant remember if i used 1 = or 2 ==) but i did try both

if (Logging = true)
{
m_msgData(d +"log on");
}

if (Logging = false)
{
m_msgData(d +"log off");
}

just to test, and it didnt matter if it was a 1 or a 0 in the Logging=0 (config.ini), it always used to first log on.

[edited by - Bmsfx on February 27, 2004 12:28:01 PM]

Share this post


Link to post
Share on other sites
okay forgot the include for the std::map
#include <map>

still getting some errors

if i use this

typedef map<int, double> myMap;
//map myMap;

string key,value;

while(getline(hfile,key,''=''))
{
// Reject blank lines

// Reject any comments you might have in the config file

if(getline(hfile,value))
{
myMap [key.c_str()] = value;
}
else
{
break; // EOF after the =

}


then i get 2 errors

error C2143: syntax error : missing '';'' before ''[''
error C2143: syntax error : missing '';'' before ''[''

the line it points too is: myMap [key.c_str()] = value;

maybe im doing something wrong (god i feel so noobie right now).
anyone can tell me ?

Share this post


Link to post
Share on other sites
quote:
Original post by bmsfx
myMap [key.c_str()] = value;



What on earth do you think that line does?? I can tell you that inplace of "[key.c_str()]" the compiler is expecting a variable name. After all, myMap is merely a type name, not a variable.


______________________________________________________________
The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ
MySite
______________________________________________________________


[edited by - Thunder_Hawk on February 27, 2004 4:08:43 PM]

Share this post


Link to post
Share on other sites
you futzed with the map thing incorrectly.

it works like so: map< type_of_key, type_of_value> you did something with a typedef (eh? where''d that come from?) and you''re mapping int''s to double''s, which is not what you want.

stick with a map<string, string> and if you need a number out of it, you can convert the string to a number later.

Share this post


Link to post
Share on other sites
quote:
Original post by bmsfx
Blew your idea didnt give errors, but it didnt detect true or false, it always only returned true (or was it false) didnt matter if it was a 1 or 0 or 10 standing in the config files.

I am aware of that. My solution was just the simplest thing he could do with the code he had (for the sake of simplicity). I wouldn''t code the way he is trying to do it. I''d go with the map too, as you said.

But yes, basically 0 is false and everything else is true. Just like bool works. I should have mentioned that though.

Cheers.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!