0

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!

2 Answers 2

5

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:

#include <fstream>   // for std::ifstream
#include <sstream>   // for std::istringstream
#include <string>    // for std::string and std::getline
#include <vector>    // for std::vector
#include <tuple>     // for std::tuple

int main()
{
    std::ifstream infile("thefile.txt");
    std::string line;

    std::vector<std::tuple<double, double, double, double>> points;

    while (std::getline(infile, line))
    {
        std::istringstream iss(line);
        double d1, d2, d3;
        char c1, c2, c3;

        if ((iss >> c1 >> d1 >> c2 >> d2 >> c3 >> d3) &&
            (c1 == '(' && c2 == ',' && c3 == ',')        )
        {
            points.emplace_back(d1, d2, d3, 1.0);
        }
        else
        {
            // error processing this line
        }
    }
}

The two errors that can occur are that either a line isn'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).

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the edit, I couldn't decided if my text file should go into code blocks :) And I will try this method. This is only for testing purposes and I know the text will be formatted in this way because I am making it.
0

You need to read these lines into strings. So, assuming you have read a line into a string, you could look at the string functions here and choose what suits you best: http://www.cplusplus.com/reference/string/string/

You could use the replace function to replace "," and ")" with "" if that's what you want. By doing this, you would be left with the 3 values separated by spaces.

2 Comments

Thanks Divya, that sounds like a decent enough plan. Replace sounds much better than going through char by char and checking against those values. If I change the text file to be simply a single float value (e.g. 0.385) on each line with no commas and no ")", what would be the best way to read them as a float in that case? Thanks again.
You could read a value into a string and then use atof to convert that value from string to double. You could then cast double to float. cplusplus.com/reference/clibrary/cstdlib/atof

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.