2015-04-03 49 views
0

即時通訊新手入門,並在我的第一個項目上工作,其中涉及讀取下面表單的.txt文件。我遇到的問題是將像素的值插入到2D動態表中,我可以稍後分析。我需要讀取第一個像素的值並將其放置到表格的第一個元素中,並將第二個像素插入到表格的第二個元素中......直到我將所有像素放在高度爲150和寬度爲250的表格中(注意這只是一個例子,維度可以根據.txt文件而改變)。從.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; 
} 

任何幫助,將不勝感激...謝謝

+0

我不太明白的輸入格式。 您將獲得寬度和高度,然後X數量的顏色,然後是X顏色值?顏色值是什麼意思? 205紅色是怎樣的?它似乎不像你使用RGB? 無論如何,您可以將每一位數據讀入一個向量/列表並重新構造它/重用它,或者您可以在1遍中創建多維向量。 – Mohammad 2015-04-03 10:05:32

+0

txt文件中的值代表圖形的圖像。因此,紅色像素用數字205表示,藍色像素用35表示,白色像素用0表示。圖像可以包含任意數量的顏色,所以前幾行提供了顏色數量及其代表值的信息。我的問題是試圖讀取這些值,並把它們放入一個多維向量,努力掙扎,因爲它只是我的第五個星期的C++ ..謝謝:) – 2015-04-03 10:09:29

回答

0

inicialize向量;矢量點; 現在創建循環

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

它必須工作

+0

謝謝你。我只是不太明白push_back如何與表格一起工作......並且我怎樣才能檢查正確的值已經到了他們應該去的地方。還有其他方法嗎? – 2015-04-03 11:12:24

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; 
} 
+0

非常感謝..真的很感激:)))) – 2015-04-03 11:32:19

相關問題