从读取的文件中分割一个单词
我正在尝试将文本文件的内容读入类的属性中。该文件的结构方式使得每一行都包含该类的一个对象所需的所有信息。一行可能看起来像:
TYC 9537 00066 1 341.76920751 -88.32499920 8.762 9.294 mc1 hd 210531 0.385 8.80 P F5 5 6440 F5 V
我一直在做的是使用 getline()
将行从文件读取到流中,在这样的函数中:
void LoadFile( std::vector<TycDat*> & target , std::fstream & source )
{
std::string line ;
while( std::getline( source , line ) )
{
target.push_back( new TycDat ( line ) ) ;
}
}
其中 TycDat
是类的名称,target
是我存储它的向量,source
是已经指向该文件的文件流。
在 TycDat
的构造函数中,我创建了一个与 std::string
参数 line
关联的 std::istringstream
并使用 >>
运算符将值按顺序读取到属性中。这在很大程度上是有效的。我必须创建一些 char
数组才能读取其中的一些数组,例如,其中 hd 210531
对应于一个 string
,其中 < code>>> 运算符将进行拆分。
我真正的问题来自最后一部分,在示例中是 8.80 P F5 5 6440
。其中说 F5 实际上是两个值,一个字母和一个数字,我需要将它们分别存储为 char 和 int。此外,末尾的 int 可能并不总是存在。
我现在正在做的是,在正确阅读 P 之后:
std::string aux_ ;
iss >> aux_ ;
TClass = aux_[0] ; // Temperature class (7)
if ( aux_.size() > 1 ) SClass = std::atoi( aux_.data()+1 ) ; // SubClass
我不喜欢那样,主要是因为我正在创建一个新的字符串
。但我不知道如何让它正常工作。问题在于将单词分开(我想我可以忽略
一个字符,然后使用TClass = iss.get()
来获取TClass
,但我不知道如何在读入之前检查下一个值是否存在。
嗯,我可以 peek
查找与 ' '
不同的值,然后使用 >>
运算符...
好吧,我会尝试并让您知道情况如何,感谢您的帮助。写完所有这些后删除问题。
I'm trying to read the contents of a text file into the attributes of a class. The file is structured in such a way that each line contains all the information needed for one object of the class. One line might look like:
TYC 9537 00066 1 341.76920751 -88.32499920 8.762 9.294 mc1 hd 210531 0.385 8.80 P F5 5 6440 F5 V
What I have been doing is read the line from the file to a stream with getline()
, in a function like so:
void LoadFile( std::vector<TycDat*> & target , std::fstream & source )
{
std::string line ;
while( std::getline( source , line ) )
{
target.push_back( new TycDat ( line ) ) ;
}
}
Where TycDat
is the name of the class, target
is the Vector I'm storing it in, and source
is the file stream already pointed to the file.
Within the constructor for TycDat
I created a std::istringstream
associated to the std::string
parameter line
and used the >>
operator to read the values in order into the attributes. This worked for the most part. I had to create a few char
arrays to be able to read some of them, where hd 210531
for example corresponds to one string
which the >>
operator would have split.
My real issue came with one of the last parts, in the example the 8.80 P F5 5 6440
. Where it says F5 are in fact two values, a letter and a number, that I need to store separately as a char and an int. Moreover, the int at the end might not always be there.
What I'm doing right now is, after reading the P correctly:
std::string aux_ ;
iss >> aux_ ;
TClass = aux_[0] ; // Temperature class (7)
if ( aux_.size() > 1 ) SClass = std::atoi( aux_.data()+1 ) ; // SubClass
I don't like that, mostly because I'm making a new string
. But I couldn't figure out how to make it work correctly. The problem is in splitting the word apart (I guess I could ignore
one character and then use TClass = iss.get()
to get TClass
, but I don't know how to check that the next value exists before reading it in.
Hmm, I could peek
to look for a value different from ' '
, then use the >>
operator....
Well, I'll try that I guess and let you know how it goes. Thanks for the help. Any other comments are appreciated. I feel bad about erasing the question after writing all this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的建议是看看 Boost.Spirit。它将允许您自然地表达输入字符串的格式,并且如果(当!)输入格式发生变化时,将使将来的更改变得更容易。作为奖励,它在运行时运行得更快!
http://www.boost。 org/doc/libs/1_44_0/libs/spirit/doc/html/spirit/introduction.html
祝你好运
My suggestion would be to have a look at Boost.Spirit. It will allow you to express the format of your input string naturally, and will make future changes easier if (when!) the input format changes. As a bonus, it'll run much faster at runtime!
http://www.boost.org/doc/libs/1_44_0/libs/spirit/doc/html/spirit/introduction.html
Good Luck