从.txt文件读取并将值输入到多维向量中

时间:2015-04-03 09:50:11

标签: c++ text input vector output

我是c ++的新手,正在处理我的第一个涉及阅读下面表格的.txt文件的项目。我遇到问题的部分是将像素的值插入到2D动态表中,我可以在以后对其进行分析。我需要读取第一个像素的值并放入表格的第一个元素,将第二个像素放入表格的第二个元素等等......直到我拥有高度为150和宽度为250的表格中的所有像素(请注意,这只是一个示例,维度可能会根据.txt文件而改变。)

250 // width pixels
150 // height en pixels
2 // number of colours
205 // colour 0
35 // colour 1
0 // value of pixel 0, background colour (white)
0 // value of pixel 1, background colour (white)
…
205 // one pixel of colour 0 (red)
…
35 // one pixel of colour 1 (blue)
…
0 // value of last pixel, background colour

到目前为止,我的代码看起来像这样(编译):

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

int main () {

    ifstream f_in;

    f_in.open("Pixmap.txt");

    int largeur;
    int hauteur;
    int nbre;

    if (f_in.is_open())
    {
        f_in >> largeur;
        f_in >> hauteur;
        f_in >> nbre;
    }


    else cerr << "Unable to open file";

    f_in.close();


    return 0;
}

任何帮助将不胜感激...谢谢

2 个答案:

答案 0 :(得分:0)

inicialize vector;矢量点; 现在创建循环

for (int i = 0; i < hauteur; i++) {
    for (j = 0; j < largeur; j++) {
        int tmp;
        f_in >>tmp;
        points.push_back(tmp);
    }
}

它必须有效

答案 1 :(得分:0)

这是一个完整的解决方案。我添加了很多评论来帮助您理解正在发生的一切。我们假设文件格式正确(即您给它正确的格式)。

使用它,颜色存储在颜色矢量中,像素数据存储在数据矢量中。我们通过文件读取我想要的所有值的一次。

您可能必须查找用于更好地理解它们的一些函数。 http://www.cplusplus.com/reference/string/string/getline/ http://www.cplusplus.com/reference/vector/vector/ http://mathbits.com/MathBits/CompSci/APstrings/APgetline.htm(这就是我们必须使用虚拟getline的原因)

我还在末尾添加了一些打印件,使用迭代器打印出数据。 如果有什么不清楚,请告诉我。

测试数据:

3
2
2
205
35
1
1
1
2
2
2

以下是该计划:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

int main() {

  ifstream f_in;

  f_in.open("Pixmap.txt");

  //If its not open return with an error
  if (!f_in.is_open())
    cerr << "Error!";

  int width;
  int height;
  int numColours;

  //Read width and height and num of colours (first 3 lines from file)
  f_in >> width;
  f_in >> height;
  f_in >> numColours;

  vector<int> colours; //we will store the colours in here

  //Read in colours
  for (int c = 0; c < numColours; ++c) { //we have a loop that iterated numColours times
    int colour;
    f_in >> colour; //read in colour
    colours.push_back(colour); //push to the back of the vector so the read will be in order
  }

  //Make multidimentional vector
  //the data(height, vector<int>(width)) means we are initilizing the size of
  //of the inner vector. There are 'height' rows (ie height number of vector<int> and each
  //vector<int> is initilised to a vector<int>(width), ie vector<int> of size width
  //All the initial values will be 0
  vector<vector<int>> data(height, vector<int>(width));

  string input; //input string for reading lines

  int i = 0;
  int j = 0;

  //We need a dummy read so our stream points to the pixel data
  //We could do this in other ways but right now we will use getline to get a line but not save it
  //This is actually an issue with using >> operator before getline (see my link to read about it)
  getline(f_in, input);

  //We use getline in a loop like this. Get line will get 1 line from the file pointed to by the stream and store it
  //In the input string
  while (getline(f_in, input)) {
    data[i][j++] = stoi(input); //We store the data we need, stoi converts strings to integers

    //Once j has reached end of vector
    if (j == width) {
      ++i; //increase i (we are at a new row now)
      j = 0; //set j = 0, the width has been reached and we want to start at 0th spot on new line
    }
  }

  //Print out colours
  int colNum = 0;
  cout << "Num of Colours: " << numColours << endl;

  for (auto itc = colours.begin(); itc != colours.end(); ++itc) {
    cout << "Colour " << ++colNum << ": " << *itc << endl;
  }

  //Print out the vector
  for (auto it = data.begin(); it != data.end(); ++it) {
    for (auto it2 = it->begin(); it2 != it->end(); ++it2) {
      cout << *it2 << " ";
    }

    cout << endl;
  }

  //close stream
  f_in.close();

  return 0;
}