C++ 从 .txt 文件中读取浮点数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8421170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:32:34  来源:igfitidea点击:

Read floats from a .txt file

c++filetextfloating-point

提问by degude

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

如何从 .t​​xt 文件中读取浮点数。根据每行开头的名称,我想读取不同数量的坐标。浮点数由“空格”分隔。

Example: triangle 1.2 -2.4 3.0

例子: triangle 1.2 -2.4 3.0

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

结果应该是: 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;
}

回答by Christian Rau

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

为什么不以通常的方式使用 C++ 流而不是所有这些getc疯狂的东西:

#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
        ...
}

回答by dasblinkenlight

This should work:

这应该有效:

string shapeName;
source >> shapeName;
if (shapeName[0] == 't') {
    float a,b,c;
    source >> a;
    source >> b;
    source >> c;
}