Using ifstream to read floats

前端 未结 2 992
忘掉有多难
忘掉有多难 2020-12-14 12:10

I\'m trying to read a series of floats from a .out file using ifstream, but if I output them afterwards, they are not correct.

This is my input code:



        
相关标签:
2条回答
  • 2020-12-14 12:48
    #include <fstream>
    #include <strtk.hpp>   // http://www.partow.net/programming/strtk
    
    std::string filename("Resources/bones.out");
    
    // assuming the file is text
    std::fstream fs;
    fs.open(filename.c_str(), std::ios::in);
    
    if(fs.fail())  return false;   
    
    const char *whitespace    = " \t\r\n\f";
    
    std::string line;
    std::vector<float> floats;
    std::vector<std::string> strings;
    float x = 0.0, y = 0.0, z = 0.0;
    std::string xs, ys, zs;
    
    // process each line in turn
    while( std::getline(fs, line ) )
    {
        // Removing beginning and ending whitespace
        // can prevent parsing problems from different line endings.
        // formerly accomplished with boost::algorithm::trim(line)
    
        strtk::remove_leading_trailing(whitespace, line);
    
    
        // strtk::parse combines multiple delimiters in these cases
    
        if( strtk::parse(line, whitespace, floats ) ) 
        {
             std::cout << "succeed" << std::endl;
             // floats contains all the values on the in as floats
        }
    
        if( strtk::parse(line, whitespace, strings) ) 
        {
             std::cout << "succeed" << std::endl;
             // strings contains all the values on the in line as strings
        }
    
        if( strtk::parse(line, whitespace, x, y, z) ) 
        {
             std::cout << "succeed" << std::endl;
             // x,y,z contain the float values.  parse fails if more than 3 floats are on the line
        }
    
        if( strtk::parse(line, whitespace, xs, ys, zs) ) 
        {
             std::cout << "succeed" << std::endl;
             // xs,ys,zs contain the strings.  parse fails if more than 3 strings are on the line
        }
    }
    

    This is how I would solve it. You can pick your way to parse the data.

    0 讨论(0)
  • 2020-12-14 13:04

    I have worked with that before and something I would do is like the code below where you read your text file line by line and use getline and a string to put the twext into variables. You don't have to ue an array as it is limited to the elements but use a vector and that way you can add dynamically.

        string xs;
        string ys;
        string zs;
        ifstream infile;
        someArray[50];
        infile.open("some file.txt");
    
        if (!infile)
        {
            cout << "no good file failed! \n" << endl;
        }
    
        while (infile.good())
        {
            for (int i = 0; i < 49; ++i)
            {
                getline(infile, xs);
                //Saves the line in xs.
                    infile >> p[i].xs;
    
                getline(infile, ys, ',');
                infile >> p[i].ys;
                getline(infile, zs, ',');
                infile >> p[i].zs;
    
            }
            //infile >> p.fromFloor; */
    
    
    
        }
    
        infile.close(); 
    }
    
    0 讨论(0)
提交回复
热议问题