读取输入文件的部分 [英] Reading parts of an input file

查看:140
本文介绍了读取输入文件的部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读取C ++中的输入文件,其结构(或缺少)将类似于 text = number 的一系列行,例如

I would like to read an input file in C++, for which the structure (or lack of) would be something like a series of lines with text = number, such as

input1 = 10
input2 = 4
set1 = 1.2
set2 = 1.e3

我想得到的行号,并抛弃休息。数字可以是整数或双精度,但我知道他们是什么时候或是其他。

I want to get the number out of the line, and throw the rest away. Numbers can be either integers or doubles, but I know when they are one or other.

我也想读它,例如

input1 =    10
input2=4
set1   =1.2
set2= 1.e3

,以便对用户更加健壮。我认为这意味着它不应该是以格式化的方式红色。

so as to be more robust to the user. I think this means that it shouldn't be red in a formatted fashion.

无论如何,有一个聪明的方法来做到这一点?

Anyway, is there a smart way to do that?

我已经尝试过下面这些,但是对我所做的事知之甚少,所以结果是预期的...没有成功。

I have already tried the following, but with minimal knowledge of what I've been doing, so the result was as expected... no success.

    #include <stdio.h>
    #include <stdlib.h>
    #include <float.h>
    #include <math.h>
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <cstdlib>
    #include <boost/lexical_cast.hpp>
    #include <string>

    using namespace std;
    using namespace boost;

    int main(){

            string tmp;
            char temp[100];

            int i,j,k;

            ifstream InFile("input.dat");

            //strtol
            InFile.getline(temp,100);
            k=strtol(temp,0,10);
            cout << k << endl;

            //lexical_cast
            InFile.getline(temp,100);
            j = lexical_cast<int>(temp);
            cout << j << endl;

            //Direct read
            InFile >> tmp >> i;
            cout << i << endl;

            return 0;
    }


推荐答案

时间。

然后在=号上分割每一行。使用流功能完成其余操作。

Simply read one line at a time.
Then split each line on the '=' sign. Use the stream functionality do the rest.

#include <sstream>
#include <fstream>
#include <iostream>
#include <string>

int main()
{
    std::ifstream    data("input.dat");
    std::string      line;

    while(std::getline(data,line))
    {
        std::stringstream    str(line);
        std::string          text;

        std::getline(str,text,'=');

        double   value;
        str >> value;
    }
}

错误检查:

#include <sstream>
#include <fstream>
#include <iostream>
#include <string>

int main()
{
    std::ifstream    data("input.dat");
    std::string      line;

    while(std::getline(data,line))
    {
        std::stringstream    str(line);
        std::string          text;
        double               value;

        if ((std::getline(str,text,'=')) &&  (str >> value))
        {
            // Happy Days..
            // Do processing.
            continue; // To start next iteration of loop.
        }
        // If we get here. An error occurred.
        // By doing nothing the line will be ignored.
        // Maybe just log an error.
    }
}

这篇关于读取输入文件的部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆