从带有单词的文本文件读取整数 [英] Reading integers from a text file with words

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

问题描述

我试图从结构化为这样的文本文件中读取整数。

  ALS 46000 
BZK 39850
CAR 38000
// ....

使用ifstream。



我考虑了两个选项。



1)使用Boost的正则表达式



2)创建一个一次性的字符串(即我读一个字,不要做任何事情,然后在分数中读)。但是,这是最后的手段。



有没有什么方法在C ++中表达我想ifstream只读取整数的文本?我不愿意使用正则表达式,如果事实证明有一个更简单的方法来完成这个。

解决方案

为什么使简单的事情复杂?



这里有什么错误:

  ifstream ss(C:\\test.txt); 

int score;
string name;
while(ss>> name>> score)
{
//用分数
做某事}
/ pre>

I'm trying to read just the integers from a text file structured like this....

ALS 46000
BZK 39850
CAR 38000
//....

using ifstream.

I've considered 2 options.

1) Regex using Boost

2) Creating a throwaway string ( i.e. I read in a word, don't do anything with it, then read in the score ). However, this is a last resort.

Are there any ways to express in C++ that I want the ifstream to only read in text that is an integer? I'm reluctant to use regular expressions if it turns out that there is a much simpler way to accomplish this.

解决方案

why to make simple things complicated?

whats wrong in this :

ifstream ss("C:\\test.txt");

int score;
string name;
while( ss >> name >> score )
{
    // do something with score
}

这篇关于从带有单词的文本文件读取整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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