如何从ASCII文件(C ++)读取数字 [英] How to read numbers from an ASCII file (C++)

查看:123
本文介绍了如何从ASCII文件(C ++)读取数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要读取以下数据文件:

I need to read in data files which look like this:

* SZA: 10.00
 2.648  2.648  2.648  2.648  2.648  2.648  2.648  2.649  2.650  2.650
 2.652  2.653  2.652  2.653  2.654  2.654  2.654  2.654  2.654  2.654
 2.654  2.654  2.654  2.655  2.656  2.656  2.657  2.657  2.657  2.656
 2.656  2.655  2.655  2.653  2.653  2.653  2.654  2.658  2.669  2.669
 2.667  2.666  2.666  2.664  2.663  2.663  2.663  2.662  2.663  2.663
 2.663  2.663  2.663  2.663  2.662  2.660  2.656  2.657  2.657  2.657
 2.654  2.653  2.652  2.651  2.648  2.647  2.646  2.642  2.641  2.637
 2.636  2.636  2.634  2.635  2.635  2.635  2.635  2.634  2.633  2.633
 2.633  2.634  2.634  2.635  2.637  2.638  2.637  2.639  2.640  2.640
 2.639  2.640  2.640  2.639  2.639  2.638  2.640  2.640  2.638  2.639
 2.638  2.638  2.638  2.638  2.637  2.637  2.637  2.634  2.635  2.636
 2.637  2.639  2.641  2.641  2.643  2.643  2.643  2.642  2.643  2.642
 2.641  2.642  2.642  2.643  2.645  2.645  2.645  2.645

将这个文件读入浮点数组的最优雅的方法是什么?

What would be the most elegant way to read this file into an array of floats?

我知道如何读取每一行到一个字符串,我知道如何使用 atof()。但是我如何做最简单的休息?

I know how to read each single line into a string, and I know how to convert the string to float using atof(). But how do I do the rest the easiest?

我听说过字符串缓冲区,这可能帮助我吗?

I've heard about string buffers, might this help me?

推荐答案

由于这是标记为C ++,最明显的方式将使用流。在我的头顶,这样的东西可能做:

Since this is tagged as C++, the most obvious way would be using streams. Off the top of my head, something like this might do:

std::vector<float> readFile(std::istream& is)
{
  char chdummy;
  is >> std::ws >> chdummy >> std::ws; 
  if(!is || chdummy != '*') error();
  std::string strdummy;
  std::getline(is,strdummy,':');
  if(!is || strdummy != "SZA") error();

  std::vector<float> result;
  for(;;)
  {
    float number;
    if( !is>>number ) break;
    result.push_back(number);
  }
  if( !is.eof() ) error();

  return result;
}

为什么 float BTW?通常, double 要好得多。

Why float, BTW? Usually, double is much better.

编辑,因为系统询问是否返回向量好主意:

Edit, since it was questioned whether returning a copy of the vector is a good idea:

对于第一个解决方案,我肯定会做的很明显。该功能的的读入文件到一个矢量,而最明显的事情一个函数做的是返回它的结果。是否这导致了显着的减速取决于很多东西(矢量的大小,功能是如何经常被称为和从那里,磁盘的速度从,编译器是否可以申请RVO这个阅读)。我不想破坏与优化显而易见的解决方案,但如果确实分析表明,这是减缓,向量应该每非const引用传递。

For a first solution, I'd certainly do the obvious. The function is reading a file into a vector, and the most obvious thing for a function to do is to return its result. Whether this results in a noticeable slowdown depends on a lot of things (the size of the vector, how often the function is called and from where, the speed of the disk this reads from, whether the compiler can apply RVO). I wouldn't want to spoil the obvious solution with an optimization, but if profiling indeed shows that this is to slow, the vector should be passed in per non-const reference.

(另请注意,C ++ 1X与右值的支持,希望很快可用通过您附近的编译器的方式,将使这种讨论毫无意义,因为它会阻止从函数返回时复制的向量。)

(Also note that C++1x with rvalue support, hopefully soon to be available by means of a compiler near you, will render this discussion moot, as it will prevent the vector from being copied upon returning from the function.)

这篇关于如何从ASCII文件(C ++)读取数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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