sscanf - how Im suppose to use it for that?

Started by
26 comments, last by MaulingMonkey 14 years ago
"
You can do like that too:

sscanf(line, "%d/%d/%d %d/%d/%d %d/%d/%d", &tmp[0], &tmp[1], &tmp[2], &tmp[3], &tmp[4], &tmp[5], &tmp[6], &tmp[7], &tmp[8]);
"

That doesnt work for me.
Advertisement
Quote:Original post by Icebone1000

Whats the problem on using sscanf if its faster btw?



Actually Im not understanding a crazy situation(at debuging looks fine, everything works as suppose), my app start consumes all my ram(4GB)! also the mouse get slow, and I need to ctrl+alt+del to close it..and that happens loading a file of 229KB! Any idea why this happens? Is this because std::vectors are actually terrible and Im better making the damn wheel that everyone loves to flame...
-___- yes, Im desperate(and my english sucks)


i guess you answered your question yourself..

scanf is very old, very chaotic designed, and definitely not failsafe. tons of ways to use it in a wrong way resulting in memory bugs or crashes.

which is why you should use iostreams instead. and they could actually be faster. why?

iostreams know at COMPILE TIME what types they will read from the file. sscanf has to parse at RUNTIME the scan-string, interpret all the parameters, and based on this, read then those values.

so if done right, iostreams should dance around scanf in performance. and definitely in savety.
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

But i just substituted all ifstream>> by sscanf and my performace power uped from 92.235176 to 39.570114 ms...how I suppose to do that little stuff of getting string from file and parsing on variables?
File_p.getline( Line, 1000 );				sscanf( Line, " %d %*c %d %*c %d %d %*c %d %*c %d %d %*c %d %*c %d", &tmpF[0], &tmpF[1], &tmpF[2], &tmpF[3], &tmpF[4], &tmpF[5], &tmpF[6], &tmpF[7], &tmpF[8] );				//File_p>>tmpF[0]>>eatslash>>tmpF[1]>>eatslash>>tmpF[2]>>tmpF[3]>>eatslash>>tmpF[4]>>eatslash>>tmpF[5]>>tmpF[6]>>eatslash>>tmpF[7]>>eatslash>>tmpF[8];//get 9 indices

i'm not using c++ since years so i can't help you in detail (and especially not with pre-c dated libraries like sscanf). it would be easy to do in .net, though.. :)

and as long as you have your memory leaks, i would not consider the performance important. how much time is it worth spending to just actually getting it working? esp. as you have an already working solution.

maybe (just maybe) maya has an actual parser written to load the files. and that would be much faster even than sscanf.

if it's performance-important, use binary files tailored down to your needs (xna does this, at compilation of your game, it compiles the media data to the fitting binary versions with only the data one needs). that would be even faster than the actual parser that you could write.


but, as you can see, i can't really help you with the problem itself. i just would never touch a function that was written and designed way before any of the actual platforms you can develop on. it's very old (older than you maybe? older than me at least i guess). and as such, very very outdated.

it's unsave in a lot of ways (you have to manually match parsing type and the actual input type you give in / it can read beyond the input string resulting in errors, etc etc..).

i'd stay with the std input version.
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

another idea could be to actually read the file like you do for sscanf (means the std::getline or what ever you use), and THEN use stringstream to read it in (so instead of ifstream>>bla>>bla>>bla, you'd use stringstream>>bla>>bla>>bla then).

maybe just the pattern in which the file gets accessed defines the performance (hdd's are very slow, which is why i moved to ssds over a year ago).

maybe try that out?



if i look at your original example, it actually looks like you're reading it all twice. first a getline, and then still from the same stream the actual inputting. instead of using the actual data you got from getline.
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

How do I get a line from ifstream to an istringstream?
std::istringstream ssLine;

std::getline( File_p, ssLine );

I get all confused with all that string, iostream, sstream mess..
as said, no c++ guru here anymore..

the simple way would be

std::ifstream input("filename");float bla,bla2,bla3;input>>bla>>bla2>>bla3;

no clue what your getline in there actually is for :)


the longer stringstream style way would be sort of like that

std::ifstream input("filename"); //could be a FILE*, too, or windows file reading routinesfloat bla,bla2,bla3;std::string inputline;std::getline(input, inputline);std::istringstream reader(inputline);reader>>bla>>bla2>>bla3;



all of the reading obviously in a loop.
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

Im still to try the istringstream method, but just figure out something that will make you surprise .

ACTUALLY, the code that makes things loading forever and ever, consuming all my damn memory, is the first one I was using(fstream):

File_p>>tmpF[0]>>eatslash>>tmpF[1]>>eatslash>>tmpF[2]>>tmpF[3]>>eatslash>>tmpF[4]>>eatslash>>tmpF[5]>>tmpF[6]>>eatslash>>tmpF[7]>>eatslash>>tmpF[8];//get 9 indices

Just tried the 229KB file again using the sscanf, it loads normaly! How the hell do you explain that!

A code mistake? it happens just to this file, the cube and the teapot file loads ok.
With the sscanf every file is loading fine! Thats just unbeliveble!
Everytime I set the string I have to istringstream.str(string) or is a permanent association ?
please show the whole file loading from the moment you open the file to the point where it crashes.

from the unclear snippets you state, i can not help you.
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

This topic is closed to new replies.

Advertisement