Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7049015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:57:41+00:00 2026-05-28T02:57:41+00:00

I know there are quite a few text-to-array questions out there, but I am

  • 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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T02:57:41+00:00Added an answer on May 28, 2026 at 2:57 am

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that there are quite a few different topics out there but I
Disclaimer: I know there are quite a few questions out there with this topic
I know there are quite a few of those kind of questions around here,
I know there are quite a few solutions for this problem but mine was
I know there are quite a few questions concerning access violations, but I have
Now I know there are quite a few questions regarding this but in general
First of all, I know there are a few quite similar questions here on
On SO, there are quite a few questions about performance profiling, but I don't
I know there have been quite a few questions about this, however, I'm still
I know there are many questions asking for this, but I have tried quite

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.