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 6902633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:49:22+00:00 2026-05-27T07:49:22+00:00

How can I read floats from a .txt file. Depending on the name at

  • 0

How can I read floats from a .txt file. Depending on the name at the begining of each line I want to read a different number of coordinates. The floats are seperated by “space”.

Example: triangle 1.2 -2.4 3.0

The result should be :
float x = 1.2 / float y = -2.4 / float z = 3.0

The file has more lines with differens shapes which can be more complex but I think if i know how to do one of them I can do the others on my own.

My Code so far:

#include <iostream>

#include <fstream>

using namespace std;

int main(void)

{

    ifstream source;                    // build a read-Stream

    source.open("text.txt", ios_base::in);  // open data

    if (!source)  {                     // if it does not work
        cerr << "Can't open Data!\n";
    }
    else {                              // if it worked 
        char c;
        source.get(c);                  // get first character

        if(c == 't'){                   // if c is 't' read in 3 floats
            float x;
            float y;
            float z;
            while(c != ' '){            // go to the next space
            source.get(c);
            }
            //TO DO ??????              // but now I don't know how to read the floats          
        }
        else if(c == 'r'){              // only two floats needed
            float x;
            float y;
            while(c != ' '){            // go to the next space
            source.get(c);
            }
            //TO DO ??????
        }                                
        else if(c == 'p'){              // only one float needed
            float x;
            while(c != ' '){            // go to the next space
            source.get(c);
            }
            //TODO ???????
        }
        else{
            cerr << "Unknown shape!\n";
        }
    }   
 return 0;
}
  • 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-27T07:49:22+00:00Added an answer on May 27, 2026 at 7:49 am

    Why not just use C++ streams the usual way instead of all this getc madness:

    #include <sstream>
    #include <string>
    
    for(std::string line; std::getline(source, line); )   //read stream line by line
    {
        std::istringstream in(line);      //make a stream for the line itself
    
        std::string type;
        in >> type;                  //and read the first whitespace-separated token
    
        if(type == "triangle")       //and check its value
        {
            float x, y, z;
            in >> x >> y >> z;       //now read the whitespace-separated floats
        }
        else if(...)
            ...
        else
            ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having issues when I try to read a float number from a file.
I'm reading in a NetCDF file and I want to read in each array
I can read XLS file with this code : string path =@c:\r\1.xlsx; OleDbConnection MyConnection
I am trying to read in with C# a file written with CArchive. From
I tried to read word by word from a file and store it in
Can someone explain to me how can I do to read some information from
I have a value I read in from a file and is stored as
I want to make certain query to mysql from C# so I can get
I'm trying to load some decimal values from a file but I can't work
I've read through the many articles here on CSS Float but still can't get

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.