I know there are quite a few text-to-array questions out there, but I am having a hard time matching those to mine.
I am not good at C++ but need to use it for my OpenGl programming.
Basically I have made an array of vec4’s (vec4 is just 4 “points” for a coord system), and then I printed them out to a text file. The file reads like so (I eliminated the last point because it is ALWAYS 1 and I figured I could add it in when reading the text file later):
( 0.26, 0385, 0.48 )
( 0.27, 0386, 0.47 )
( 0.28, 0387, 0.46 )
( 0.29, 0388, 0.45 )
So every line is separated by one endl;
Now I want to read this text file and make it back into a vec4 array. Whats the easiest way to do this avoiding the ‘(‘, ‘,’, and ‘)’? I could probably write huge long nested if/else to weed these unwanted characters out, but it would be quite inefficient and im sure someone out there has a smarter way.
Any suggestions? Help?
Thanks!
Well, if the format is absolutely sure to be like this, then you can do a naive parse, embedded here in the standard line-based input idiom:
The two errors that can occur are that either a
lineisn’t parsable into the required tokens, or that the delimiters aren’t as expected. It’s up to you how you want to handle that.If you need more flexibility, you could process each line with a
<regex>, or you could even write a far more powerful general parser like Boost.Qi (though that’s definitely overkill).