c++;从文本文件中读取数字,忽略注释 我在这个网站上看到了很多解决方案,并且从C++的文本文件中阅读教程,但是还没有找到解决问题的办法。我是C++新手,所以我觉得我很难把一些文档整理一下,以弄清楚这一切。p>

c++;从文本文件中读取数字,忽略注释 我在这个网站上看到了很多解决方案,并且从C++的文本文件中阅读教程,但是还没有找到解决问题的办法。我是C++新手,所以我觉得我很难把一些文档整理一下,以弄清楚这一切。p>,c++,parsing,inputstream,C++,Parsing,Inputstream,我试图做的是读取文本文件编号,同时忽略文件中用“#”表示的注释。因此,示例文件如下所示: #here is my comment 20 30 40 50 #this is my last comment 60 70 80 90 当没有任何注释时,我的代码可以很好地读取数字,但我对解析流的理解不够透彻,无法忽略注释。这是一种黑客解决方案 /////////////////////// Read the file /////////////////////// std::string line;

我试图做的是读取文本文件编号,同时忽略文件中用“#”表示的注释。因此,示例文件如下所示:

#here is my comment
20 30 40 50
#this is my last comment
60 70 80 90
当没有任何注释时,我的代码可以很好地读取数字,但我对解析流的理解不够透彻,无法忽略注释。这是一种黑客解决方案

/////////////////////// Read the file ///////////////////////
std::string line;
if (input_file.is_open())
{
    //While we can still read the file
    while (std::getline(input_file, line))
    {
        std::istringstream iss(line);
        float num; // The number in the line

        //while the iss is a number 
        while ((iss >> num))
        {
            //look at the number
        }
    }
}

else
{
    std::cout << "Unable to open file";
}
/////////////////////// done reading file /////////////////
//阅读该文件///////////////////////
std::字符串行;
if(input_file.is_open())
{
//而我们仍然可以读取文件
while(std::getline(输入文件,行))
{
标准::istringstream iss(线);
float num;//行中的数字
//而国际空间站是一个数字
而((iss>>num))
{
//看看号码
}
}
}
其他的
{

std::cout如果您的文件总是在第一列中包含
,那么只要测试一下,如果行以
开头,如下所示:

while (std::getline(input_file, line))
{
    if (line[0] != "#" )
    {
        std::istringstream iss(line);
        float num; // The number in the line

        //while the iss is a number 
        while ((iss >> num))
        {
            //look at the number
        }
    }
}

明智的做法是修剪前导和尾随空格行,如下图所示,例如:

如果您的文件总是在第一列中包含
,那么只需测试,如果该行以
开头,如下所示:

while (std::getline(input_file, line))
{
    if (line[0] != "#" )
    {
        std::istringstream iss(line);
        float num; // The number in the line

        //while the iss is a number 
        while ((iss >> num))
        {
            //look at the number
        }
    }
}
不过,明智的做法是修剪前导和尾随空格行,如下图所示,例如:

可以使用流本身作为传入缓冲区来替代“读取aline并将其解析为字符串”:

while(input_file)
{
    int n = 0;

    char c; 
    input_file >> c; // will skip spaces ad read the first non-blank

    if(c == '#')
    {
        while(c!='\n' && input_file) input_file.get(c);
        continue; //may be not soooo beautiful, but does not introduce useless dynamic memory
    }

    //c is part of something else but comment, so give it back to parse it as number
    input_file.unget(); //< this is what all the fuss is about!
    if(input_file >> n)
    { 
        // look at the nunber
        continue;
    }

    // something else, but not an integer is there ....
    // if you cannot recover the lopop will exit 
}
while(输入文件)
{
int n=0;
字符c;
input_file>>c;//将跳过空格并读取第一个非空
如果(c='#')
{
而(c!='\n'&&input_file)input_file.get(c);
continue;//可能不太漂亮,但不会引入无用的动态内存
}
//c是除注释之外的其他内容的一部分,所以将其返回以将其解析为数字
input_file.unget();//<这就是所有的大惊小怪!
如果(输入文件>>n)
{ 
//看那个圆圆的
继续;
}
//还有别的,但不是整数。。。。
//如果无法恢复,lopop将退出
}
除了“读取aline并将其解析为字符串”之外,还可以使用流本身作为传入缓冲区:

while(input_file)
{
    int n = 0;

    char c; 
    input_file >> c; // will skip spaces ad read the first non-blank

    if(c == '#')
    {
        while(c!='\n' && input_file) input_file.get(c);
        continue; //may be not soooo beautiful, but does not introduce useless dynamic memory
    }

    //c is part of something else but comment, so give it back to parse it as number
    input_file.unget(); //< this is what all the fuss is about!
    if(input_file >> n)
    { 
        // look at the nunber
        continue;
    }

    // something else, but not an integer is there ....
    // if you cannot recover the lopop will exit 
}
while(输入文件)
{
int n=0;
字符c;
input_file>>c;//将跳过空格并读取第一个非空
如果(c='#')
{
而(c!='\n'&&input_file)input_file.get(c);
continue;//可能不太漂亮,但不会引入无用的动态内存
}
//c是除注释之外的其他内容的一部分,所以将其返回以将其解析为数字
input_file.unget();//<这就是所有的大惊小怪!
如果(输入文件>>n)
{ 
//看那个圆圆的
继续;
}
//还有别的,但不是整数。。。。
//如果无法恢复,lopop将退出
}

如果这只是一个用途,那么对于像您这样的面向行的输入 最简单的解决方案就是从你刚才看到的行中去掉注释 阅读:

一个更通用的解决方案是使用过滤streambuf之类的东西 比如:

在这种情况下,您甚至可以放弃
getline

FilterCommentsStreambuf filter( input_file );
double num;
while ( input_file >> num || !input_file.eof() ) {
    if ( ! input_file ) {
        //  Formatting error, output error message, clear the
        //  error, and resynchronize the input---probably by
        //  ignore'ing until end of line.
    } else {
        //  Do something with the number...
    }
}
(在这种情况下,我发现还可以在中跟踪行号
过滤器commentsstreambuf
。这样您就有了它的错误
消息。)

如果这只是一个用途,那么对于像您这样的面向行的输入 最简单的解决方案就是从你刚才看到的行中去掉注释 阅读:

一个更通用的解决方案是使用过滤streambuf之类的东西 比如:

在这种情况下,您甚至可以放弃
getline

FilterCommentsStreambuf filter( input_file );
double num;
while ( input_file >> num || !input_file.eof() ) {
    if ( ! input_file ) {
        //  Formatting error, output error message, clear the
        //  error, and resynchronize the input---probably by
        //  ignore'ing until end of line.
    } else {
        //  Do something with the number...
    }
}
(在这种情况下,我发现还可以在中跟踪行号
过滤器commentsstreambuf
。这样您就有了它的错误
消息。)

line.assign(line.substr(0,line.find('#'));
(作为while循环中的第一条语句)这是快速进行必要更改的一种方法。这非常非常简单。您说您对上面的代码理解不够好,无法对其进行修改。我认为在尝试其他操作之前,您需要花一些时间来理解这些代码。您是否尝试过在文件中提供注释?如前所述,代码将忽略文件的任何部分第一部分后面的行不是一个有效的数字,包括注释。好吧,我认为@BartvanIngenSchenau是对的,这是我的直觉,但我得到了一些奇怪的行为,现在我认为与解析无关。我没有在这里展示的是,我使用文件输入绘制了一组几何体,有时我在屏幕上画了一条红线。所以我的想法是,这可能是因为它在做一些奇怪的事情并阅读评论,但现在我认为这是另一回事。所以我要探索一些其他元素,谢谢大家。
line.assign(line.substr(0,line.find('#'));
(作为while循环中的第一条语句)这是快速进行必要更改的一种方法。这非常非常简单。您说您对上面的代码理解不够好,无法对其进行修改。我认为在尝试其他操作之前,您需要花一些时间来理解这些代码。您是否尝试过在文件中提供注释?如前所述,代码将忽略文件的任何部分第一部分后面的行不是一个有效的数字,包括注释。好吧,我认为@BartvanIngenSchenau是对的,这是我的直觉,但我得到了一些奇怪的行为,现在我认为与解析无关。我没有在这里展示的是,我使用文件输入绘制了一组几何体,有时我在屏幕上画了一条红线,所以我的想法是可能是贝卡