从文件读取数据到数组

时间:2013-06-09 17:04:09

标签: c++ file-io

我在文件上写了以下数据。我想忽略开头的所有零,但是从数组中的181开始,单元格中的每个数字都可以轻松使用它。

我知道如何将数据放入数组但是我怎么能忽略所有这些零?

0 177 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 181 98 1 2 28 0 104 93 165 3 7 110 239 5 172 164 176 29 56 147 4 0 234 215 3 0 166 6 0 0 78 5 0 0 164 145 181 98 1 2 28 0 80 97 165 3 7 110 239 5 172 164 176 29 56 147 4 0 234 215 3 0 169 6 0 0 78 5 0 0 147 117 181 98 1 2 28 0 56 101 165 3 7 110 239 5 172 164 176 29 56 147 4 0 234 215 3 0 173 6 0 0 81 5 0 0 134 109 181 98 1 2 28 0 32 105 165 3 7 110 239 5 172 164 176 29 56 147 4 0 234 215 3 0 181 6 0 0 85 5 0 0 126 137 181 98 1 2 28 0 8 109 165 3 7 110 239 5 172 164 176 29 56 147 4 0 234 215 3 0 182 6 0 0 87 5 0 0 109 101

2 个答案:

答案 0 :(得分:1)

我不确定我理解你的问题,所以我会发布多个答案。选择适合您问题描述的那个。

案例1:忽略181之前(或之前和之后)的所有内容:

#include <iostream>
#include <fstream>
#include <vector>

int main() {
  std::ifstream in("input.txt");
  std::vector<int> vec;
  int reached_181 = 0, x;

  while(in >> x) {
    if(x == 181) reached_181 = 1;
    if(reached_181) vec.push_back(x);
    // if you also want to neglect 181 then just change the order of the two commands
    // if(reached_181) vec.push_back(x);
    // if(x == 181) reached_181 = 1;
  }

  for(std::vector<int>::size_type i=0; i<vec.size(); ++i) {
    std::cout << vec[i] << " ";
  }

  return 0;
}

案例2:181之前忽略每个

#include <iostream>
#include <fstream>
#include <vector>

int main() {
  std::ifstream in("input.txt");
  std::vector<int> vec;
  int reached_181 = 0, x;

  while(in >> x) {
    if(x == 181) reached_181 = 1;
    if(reached_181 || x) vec.push_back(x);
  }

  for(std::vector<int>::size_type i=0; i<vec.size(); ++i) {
    std::cout << vec[i] << " ";
  }

  return 0;
}

案例3:忽略输入文件中的所有零

#include <iostream>
#include <fstream>
#include <vector>

int main() {
  std::ifstream in("input.txt");
  std::vector<int> vec;
  int x;

  while(in >> x) {
    if(x) vec.push_back(x);
  }

  for(std::vector<int>::size_type i=0; i<vec.size(); ++i) {
    std::cout << vec[i] << " ";
  }

  return 0;
}

答案 1 :(得分:0)

试试这个:

#include <fstream>
#include <vector>
#include <iostream>

int main()
{
    std::vector<int> v;
    std::fstream out("out.txt"); // name of your file
    bool hit;

    for (int n; (out >> n);)
    {
        if (n == 181 && !hit)
            hit = true;
        if (!hit)
            if (n)
                v.push_back(n);

        if (hit)
            v.push_back(n);
    }

    typedef std::vector<int>::const_iterator iter_type;

    for (iter_type it = v.begin(); it != v.end(); ++it)
        std::cout << *it << std::endl;

}