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

  • SEARCH
  • Home
  • 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 6857763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:04:09+00:00 2026-05-27T02:04:09+00:00

I need help loading a custom file format into my program made in c++…

  • 0

I need help loading a custom file format into my program made in c++…
I know there’s a simple way of doing this but I think I’m using the
wrong terms to search for it online…

My custom format for 3d objects is as follows:

NumVerts 6
//verts (float)
-1
-1
0
1
-1
0
-1
1
0
1
-1
0
1
1
0
-1
1
0
//texture (float)
0
0
1
0
0
1
1
0
1
1
0
1
//index (int)
0
1
2
1
3
2

And that is a quad… (yeas; I know… horrible format… but it’s what I’m using for an android game).

I want to make a function in c++ for my editor (SDL + OpenGL for windows) that loads these files into data… Unfortunately though I know how to export this format with C++, I can’t figure out how to import them… I wish to use the fstream commands…

If someone could quickly write out a simple version I’d be really thankful.

I just it to do the following:

  • Find text file from input string
  • read “NumVerts” and grab the integer written after it
  • loop through the next (NumVerts*3) lines and grab each number as a float
  • loop though the next (NumVerts*2) lines and grab each number as a float
  • loop through the next (NumVerts*1) lines and grab each number as an Int
  • (skip any line that starts with “//”)
  • close file.

Thank you for reading and any help would be really good right now… or a relivent link that is quite simple and reads strings from files and grabs numbers from them to be placed into memory…

I really just want to finish this game and it’s getting really stressful trying to locate helpful tutorials.

Update: updated the script… I accidently forgot to seperate 1’s and 0’s…

  • 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-27T02:04:09+00:00Added an answer on May 27, 2026 at 2:04 am

    Hope this helps. Incidentally, you have the wrong number of vertex components – you need 18 of them.

    #include <algorithm>
    #include <exception>
    #include <fstream>
    #include <iostream>
    #include <string>
    #include <vector>
    
    #include <boost/algorithm/string/trim.hpp>
    #include <boost/lexical_cast.hpp>
    using boost::lexical_cast;
    
    int load_3d_object(const std::string& filename,
                       std::vector<float>& vertComponents,
                       std::vector<float>& texComponents,
                       std::vector<int>& indices)
    {
        std::ifstream fs(filename.c_str());
        std::string line;
        if(!std::getline(fs, line))
        {
            throw std::runtime_error("The input file is empty");
        }
    
        if(line.substr(0,8) != "NumVerts")
        {
            throw std::runtime_error("The first line must start with NumVerts");
        }
    
        // Extract the number of vertices.
        int numVerts = lexical_cast<int>(line.substr(line.find(' ') + 1));
    
        // Read in the vertex components, texture components and indices.
        while(std::getline(fs, line))
        {
            boost::trim(line);
            if(line.substr(0,2) == "//") continue;
    
            if((int)vertComponents.size() < numVerts * 3)       vertComponents.push_back(lexical_cast<float>(line));
            else if((int)texComponents.size() < numVerts * 2)   texComponents.push_back(lexical_cast<float>(line));
            else                                                indices.push_back(lexical_cast<int>(line));
        }
    
        return numVerts;
    }
    
    int main()
    {
        std::vector<float> vertComponents;
        std::vector<float> texComponents;
        std::vector<int> indices;
        try
        {
            int numVerts = load_3d_object("input.txt", vertComponents, texComponents, indices);
        }
        catch(std::exception& e)
        {
            std::cout << e.what() << '\n';
        }
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need help with eager loading in with Linq in NHibernate 3 trunk version.
I need some help on showing Progress bar upon loading data on my DataGridView.
I need help on timing issues between CSS file and HTML code from AJAX.
I need help in displaying HTML File in webbrowser in Windows phone 7 app.
Hi i need help with UIScrollView.. i m loading tens views onto my scroll
I'm loading images with this class. I need help on how to stop them
I need some help to get the ContentType of a file that is uploaded
I'm new to WPF, and I need some help. I have a slow-loading WPF
Friend's I need help on loading the content alone in my page without going
Need some help or some advice with entity framework v4. As you already know,

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.