存储文本文件转换为类 [英] Storing text file into a class

查看:158
本文介绍了存储文本文件转换为类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在读的字符转换成一个类,它看起来像这样

I am currently trying to read in characters into a class that looks like this

 struct data {
    string segment; string name;
    double length; double radius; double wall_thickness;
    double young_modulus; double compliance;

};

我也有一个载体,它包含以下元素:

I also have a vector that contains the following elements:

  2A
  Aorta_Ascendens
  2
  1.47
  .164
  4
  53.4
  2B
  Aorta_Ascendens
  2
  1.44
  .161
  4
  51.0
  3A

我想读的文本文件转换成各个部分,目前这是我的算法通过文本文件连续读取并分别添加的每个部分。

I want to read in the text file into each part, and currently this is my algorithm to continuously read through the text file and add each part respectively.

int place = 0;

        while (place != temp_data.size()){
            int counter = 0;
            for (counter; counter <= 7; ++counter){
                istringstream is(temp_data[place + counter]);

                if (counter == 0){ is >> a.segment; }
                if (counter == 1){ is >> a.name; }
                if (counter == 2){ is >> a.length; }
                if (counter == 3){ is >> a.radius; }
                if (counter == 4){ is >> a.wall_thickness; }
                if (counter == 5){ is >> a.young_modulus; }
                if (counter == 6){ is >> a.compliance; }

            }
            counter = counter - 1;     //since the next segment is at    temp_data[7], must subtract one to have place = 7.
            place = counter + place;

            v.push_back(a);
        }

我跑进正试图找到一种方法来确保文本的右边部分的问题进入对象的右侧部分。对于前七行文本的对象应该是这样的:

The problem I ran into is trying to find a way to make sure the right part of the text goes into the right part of the object. for the first seven lines of text the object should be like this :

segment: 2A
name: Aorta_Ascendens
length: 2
radius: 1.47
wall_thickness: .164
young modulus: 4
compliance: 53.4

这应该对整个文本文件的同样的方法重复。 temp_data是,认为需要添加到对象的元素的载体,但我不能找到一个好办法,通过矢量不断循环,把元素融入到正确的地方。

This should repeat the same way for the whole of the text file. temp_data is a vector that holds the elements that need to be added into the object, but I cant find a good way to continually cycle through the vector and put the elements into the right place.

我提出的算法有一个计数器和一个占位,计数器将循环通过矢量点和占位将举行点为所述第一对象的数据成员,在这种情况下,段。在算法结束时,地方应等于temp_data的大小,并获得圈外。但我有一个问题,当它编译和运行,它似乎是把错误的元素进错了对象成员。

The algorithm I made has a counter and a place holder, the counter would cycle through the vector points and the place holder would hold spot for the first object's data member, in this case segment. At the end of the algorithm, place should equal the size of temp_data and get out of the loop. But I am having a problem when it compiles and runs, it seems to be putting the wrong element into the wrong object member.

任何想法?

推荐答案

有一个简单,干净的方式来处理,这是写一个自定义的流运算符:

A simple, clean way to approach this is to write a custom streaming operator:

struct data {
    string segment; string name;
    double length; double radius; double wall_thickness;
    double young_modulus; double compliance;

    friend std::istream& operator>>(std::istream& is, data& d)
    {
        return is >> d.segment >> d.name >> d.length >> d.radius >> d.wall_thickness
                  >> d.young_modulus >> d.compliance;
    }
};

没有 temp_data istringstream 柜台

要使用这样的:

data d;
while (input_stream >> d)
    v.push_back(d);
if (input_stream.fail())
    ...print error / exit or whatver...
else
    ...use v...

(还有更多的声明方式使用标准算法和 back_inserter 迭代器从数据流复制到向量,但恕我直言浅显好)

(There are more declarative ways to use Standard algorithms and back_inserter iterators to copy from the stream to the vector, but IMHO simple and obvious is good)

这篇关于存储文本文件转换为类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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