简体   繁体   中英

Reading from a .txt file and inputting values into multidimensional vector

im new to c++ and working on my first project which involves reading a .txt file of the form below. The part i am having trouble with is inserting the values of the pixels into a 2D dynamic table which i can later analyse. I need to read the value of the first pixel and place into the first element of the table and the second pixel into the second element of the table etc... until i have all the pixels in a table of height 150 and width 250 (note this is just an example, the dimensions can change depending on the .txt file).

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

So far my code looks like this(which compiles):

#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;
}

Any help would be appreciated...thanks

inicialize vector; vector points; now create loop

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

it must works

Heres a complete solution. I added a lot of comments to help you understand everything that is going on. We assume the file is properly formatted (ie you give it the proper format).

Using this, the colours are stored in the colour vector and the pixel data in the data vector. We go through the file 1 time reading in all the values we want.

You may have to lookup some functions used to better understand them. http://www.cplusplus.com/reference/string/string/getline/ http://www.cplusplus.com/reference/vector/vector/ http://mathbits.com/MathBits/CompSci/APstrings/APgetline.htm (this is why we had to use a dummy getline)

I also added some prints at the end to print out the data using iterators. If anything is unclear, let me know.

Test data:

3
2
2
205
35
1
1
1
2
2
2

Here is the program:

#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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM