从文件中读取数字,其中包括数字以外的其他格式

时间:2018-10-16 16:35:27

标签: ifstream

我的代码适用于此文件(numbers.txt)

1 2 3 4 5 6 7 8 9 10。 但是,此版本的文件无法使用

{{1},{2},{3},{4},{5},{6},{7},{8},{9},{10}}。

我将如何使用ifstream仅输入数字,而不输入方括号或逗号?某种for循环做出区别?如果这很简单,我深表歉意。我在中断了很长时间之后又重新开始编码。

#include <iostream>
#include <iterator>
#include <fstream>
#include <vector>
#include <algorithm>

int main()
{
  std::ifstream is("numbers.txt");
  std::istream_iterator<double> start(is), end;
  std::vector<double> numbers(start, end);
  std::cout << "Read " << numbers.size() << " numbers" << std::endl;

  // print the numbers to stdout
  std::cout << "numbers read in:\n";
  std::copy(numbers.begin(), numbers.end(),
        std::ostream_iterator<double>(std::cout, " "));
  std::cout << std::endl;

}

0 个答案:

没有答案
相关问题