last day (15 days later) » 

8:40 PM
hey
 
Ohay, inviting crowd
 
oh okay
 
I figured ideone.com/x96O1j would be easiest for him to understand, but in reality, he really should be parsing the file correctly in the first place.
 
they both make sense
i'm not sure what you mean with parsing the file - my google skills came up with reading each line and assigning to a map
 
user1804599
hi
 
8:46 PM
hello :P
 
fscanf(file, "button%d_%c = %d", &number, &key, &val);
The hard part to deal with is converting from `key` to the actual field in the structure you care about.
 
what would be held in &key, and &val ?
 
@Dave ideone.com/EQoyb7 (see the input/output at the bottom)
 
`key` would hold the `x`, `y` (whatever) from the file. `val` would hold the `300`, `500`, etc. You'd then probably use a switch statement to translate:
case (key) {
case 'x': button[number].x = val; break;
case 'y': button[number].y = val; break;
}
etc.
 
ah i see
isn't key still a string type?
 
8:50 PM
@Dave In Jerry's code, key is a character
@JerryCoffin values are double by the way
 
a char basically?
 
@Dave Yes.
 
ok - my i ask the benefit to fscan over the reading each line method that i currently use ?
 
@MooingDuck Oh -- hadn't noticed. In that case, change from %d to %f, of course.
 
@Dave its faster, and actually reads data rather than having maps of strings, which makes everything else MUCH faster and use MUCH less memory.
 
8:54 PM
@Dave I'm not sure exactly what you're currently using, but the apparent benefit would be the ability to read in data for an arbitrary number of buttons, then process them in a loop.
 
in Lounge<C++>, 30 mins ago, by Dave
well its grabbed from a text file with the settings which is in the format of:

Button1_x = 300
Button1_y = 100
 
    This is the current method the function for it:

    std::map<string,string> loadSettings(std::string file_path) {
	ifstream file(file_path);
	if (file == false){ exit(1);}
	string line;
    std::map<string, string> config;

	while(std::getline(file, line))
    {
        int pos = line.find('=');
        if(pos != string::npos)
        {
            string key = line.substr(0, pos);
            string value = line.substr(pos + 1);
            config[trim(key)] = trim(value);
			 }
	}
	return (config);
 
@MooingDuck Yeah, I saw that, but it doesn't seem to include the code to read from the file.
 
@Dave that code looks awfully familiar...
 
@Dave Okay, compared to that the obvious benefit is being shorter, simpler, and producing a better result. The obvious disadvantage is fscanf.
 
8:56 PM
i believe it was you who confired my trim functions @MooingDuck :P
 
@Dave yeah, I remember that. Can't find the questions now
 
disadvantage ? how so
 
@Dave fscanf isn't type-safe. If you pass it the wrong type, it crashes or does random things.
 
ah so if some one gets the config file wrong itll crash
does it allow a error handling situation to prevent a crash ?
 
@MooingDuck That, and the fact that format strings are a strange little language of their own that almost nobody (including me, any more) really knows how to use well.
@Dave No -- you need to ensure the types you specify in the format string match the types you pass as the other parameters.
 
8:59 PM
@Dave not if you pass the wrong type.
@Dave no, if someone gives the wrong config file it wont crash, it will say it failed to read. That's safe.
 
ah ok so i can use default settings in such an situation
 
std::string word;
printf("%d", word); // %d means integer, we pass a string, so it crashes.
 
ok i understand :P
 
Something like: int x; fscanf("%f", &x); can crash, because %f tells it float, but you've passed an int. If you tell it to read an int and the config file contains (say) a "z", the read will just fail.
 
i would of thought an int would be okay to parse to a float
theres no loss of data
 
9:03 PM
@Dave There can be, but in this case, it's getting a pointer to an int, and trying to treat it as a pointer to a float. They may (for example) have different alignment requirements, so trying to write it as a float fails completely. In other cases, it might write past the end of the space you provided, overwriting something else in the process.
 
ok
shouldn't be too hard to keep the data types correct
 
@Dave In all honesty, it generally isn't -- and if you're using gcc, it can/will warn you about mismatches, as long as you're using a string literal as the format (which you almost always do).
 
yeh VS 2010 informs me for sure. right im guna go update my settings loader file to this new method :) thanks for the advice
 
@Dave That's why C++ was invented
 
I'm pretty sure VC++ doesn't have the kind of warning I'm talking about.
 
9:07 PM
@JerryCoffin you'd be wrong. I know MSVC9 does with warning level 4 at least. Not sure about lower warning levels or versions.
 
@MooingDuck Really? When was it added?
 
express version might not - but pro / ultimate edition does
 
@JerryCoffin or maybe what Dave said, I dunno.
 
you can set the warning level you want to be alerted for
i tend to ignore warnings for time being
most of them are just conversion warnings for possible data loss
 
According to this answer it was added in MSVC9, only in Express Edition, when the /analyze option is specified.
 
9:12 PM
@MooingDuck Ah, okay. Doing a quick check with VC9 (Pro edition) with /W4 doesn't give a warning (except the general warning that scanf may be unsafe). A quick check with 2012 with /analyze does give a warning though. Thanks for the correction.
 
MSDN says it was added in MSVC8 (2005)
 
anything with microsoft is a conspiracy
 
@Dave say what now? I rather think not
 
lol i proved it with a picture in c++ lounge :P
 
@Dave the picture that was completely ignored?
 
9:19 PM
lol
it was imprinted in their minds its all good
but that is also because they talking about women
which takes priority
 
@Dave if Microsoft was involved with conspiracies, you'd think they'd be doing better business, instead of losing market share in every market they're in.
 
Ell
poor microsoft :'(
 

last day (15 days later) »