2011-01-28 115 views
1

我解析矩陣文件時遇到了一些麻煩。它看起來某事像這樣:從文件創建二維數組時遇到的麻煩

# Matrix made by matblas from blosum62.iij 
# * column uses minimum score 
# BLOSUM Clustered Scoring Matrix in 1/2 Bit Units 
# Blocks Database = /data/blocks_5.0/blocks.dat 
# Cluster Percentage: >= 62 
# Entropy = 0.6979, Expected = -0.5209 
    A R N D C Q E G H I L K M F P S T W Y V B Z X * 
A 4 -1 -2 -2 0 -1 -1 0 -2 -1 -1 -1 -1 -2 -1 1 0 -3 -2 0 -2 -1 0 -4 
R -1 5 0 -2 -3 1 0 -2 0 -3 -2 2 -1 -3 -2 -1 -1 -3 -2 -3 -1 0 -1 -4 
N -2 0 6 1 -3 0 0 0 1 -3 -3 0 -2 -3 -2 1 0 -4 -2 -3 3 0 -1 -4 
D -2 -2 1 6 -3 0 2 -1 -1 -3 -4 -1 -3 -3 -1 0 -1 -4 -3 -3 4 1 -1 -4 
C 0 -3 -3 -3 9 -3 -4 -3 -3 -1 -1 -3 -1 -2 -3 -1 -1 -2 -2 -1 -3 -3 -2 -4 
Q -1 1 0 0 -3 5 2 -2 0 -3 -2 1 0 -3 -1 0 -1 -2 -1 -2 0 3 -1 -4 
E -1 0 0 2 -4 2 5 -2 0 -3 -3 1 -2 -3 -1 0 -1 -3 -2 -2 1 4 -1 -4 
G 0 -2 0 -1 -3 -2 -2 6 -2 -4 -4 -2 -3 -3 -2 0 -2 -2 -3 -3 -1 -2 -1 -4 
H -2 0 1 -1 -3 0 0 -2 8 -3 -3 -1 -2 -1 -2 -1 -2 -2 2 -3 0 0 -1 -4 
I -1 -3 -3 -3 -1 -3 -3 -4 -3 4 2 -3 1 0 -3 -2 -1 -3 -1 3 -3 -3 -1 -4 
L -1 -2 -3 -4 -1 -2 -3 -4 -3 2 4 -2 2 0 -3 -2 -1 -2 -1 1 -4 -3 -1 -4 
K -1 2 0 -1 -3 1 1 -2 -1 -3 -2 5 -1 -3 -1 0 -1 -3 -2 -2 0 1 -1 -4 
M -1 -1 -2 -3 -1 0 -2 -3 -2 1 2 -1 5 0 -2 -1 -1 -1 -1 1 -3 -1 -1 -4 
F -2 -3 -3 -3 -2 -3 -3 -3 -1 0 0 -3 0 6 -4 -2 -2 1 3 -1 -3 -3 -1 -4 
P -1 -2 -2 -1 -3 -1 -1 -2 -2 -3 -3 -1 -2 -4 7 -1 -1 -4 -3 -2 -2 -1 -2 -4 
S 1 -1 1 0 -1 0 0 0 -1 -2 -2 0 -1 -2 -1 4 1 -3 -2 -2 0 0 0 -4 
T 0 -1 0 -1 -1 -1 -1 -2 -2 -1 -1 -1 -1 -2 -1 1 5 -2 -2 0 -1 -1 0 -4 
W -3 -3 -4 -4 -2 -2 -3 -2 -2 -3 -2 -3 -1 1 -4 -3 -2 11 2 -3 -4 -3 -2 -4 
Y -2 -2 -2 -3 -2 -1 -2 -3 2 -1 -1 -2 -1 3 -3 -2 -2 2 7 -1 -3 -2 -1 -4 
V 0 -3 -3 -3 -1 -2 -2 -3 -3 3 1 -2 1 -1 -2 -2 0 -3 -1 4 -3 -2 -1 -4 
B -2 -1 3 4 -3 0 1 -1 0 -3 -4 0 -3 -3 -2 0 -1 -4 -3 -3 4 1 -1 -4 
Z -1 0 0 1 -3 3 4 -2 0 -3 -3 1 -1 -3 -1 0 -1 -3 -2 -2 1 4 -1 -4 
X 0 -1 -1 -1 -2 -1 -1 -1 -1 -1 -1 -1 -1 -1 -2 0 0 -2 -1 -1 -1 -1 -1 -4 
* -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 1 

這是更大的計劃的一部分,但首先我想將它變成一個類之前單獨檢查。所以我的代碼如下所示:

#include <iostream> 
#include <fstream> 
#include <string> 

using namespace std; 

char readfile(char * file) 
{ 
    int lines, cols; 
    char matrix[30][30]; 
    ifstream ifs(file, ios::in); 
    ifs.precision(2); 
    ifs.setf(ios::fixed, ios::showpoint); 
    ifs >> matrix[lines][cols]; 
    while(!ifs.eof()) 
    { 
     string linijka; 
     getline(ifs, linijka); 
     if (linijka[0] != '#') 
     { 
      for (lines = 0; lines < 30; lines++) 
      { 
       for (cols = 0; cols < 30; cols++) 
       { 
        return matrix[lines][cols]; 
       } 
      } 
     } 
    } 

    ifs.close(); 
} 

int main(int argc, char * argv[1]) 
{ 
    cout << "Matrix:\n" << readfile(argv[1]) << endl; 
    return 0; 
} 

所有內容都編譯時沒有錯誤。不幸的是矩陣是空的。執行它後,我收到了這樣的:

[email protected]:$ ./matpars submat/BLOSUM62.txt 
Matrix: 

我需要它從單元格獲得標記e。 G。 [T] [G]。

任何想法?我會非常感激。 ;-)

回答

3

你的代碼中有一個根本性的問題:一旦它到達return語句,函數將結束,和您的文件的其餘部分將不被解析。

這就是爲什麼你沒有看到任何東西;它只會打印出文件第一行中的第一個字符,而這恰好是第一個A列標記之前的空格。

編輯:其實,仔細一看,似乎還有一個更重要的問題。您似乎期待第一行ifs >> matrix行將整個文件加載到您的陣列中,而這是它無法做到的。

你似乎是一個開始的程序員。說實話,我建議你從比C++更簡單的語言開始,比如Python或Ruby。

+0

那麼我該如何在這個矩陣中存儲數據呢?我不需要像標準輸入那樣打印它。我只需要將它存儲起來,以便按程序搜索分數(矩陣文件中的數字)按列和行。 – 2011-01-28 17:23:12

+0

'ifs >>矩陣'行屬於內部循環,現在您有'return'行。那裏的'>>'操作符一次只能提取一個數字。此外,你也不需要`getline`調用;那是在`linijka`變量中存儲了一整行輸入,但是根本沒有使用該行,只是將其丟棄。 – DSimon 2011-01-28 17:26:55

1

我感覺這是家庭作業,所以我不想徹底爲您解決。 :-)然而,爲了幫助你,下面是用C++ ish風格編寫的Python實現:

#!/usr/bin/python 

import sys 

def readfile(fname): 
    # A dynamic array; a similar thing in C++ is vector<int>. 
    # If you want a two-dimensional dynamic array in C++, it's a vector< vector<int> > 
    # It's not as efficient as a fixed-size C-style array, but much more convenient! 
    mat = [] 
    fh = file(fname) 

    for line in fh: # Read through the file line by line 
    cells = line.split() # This is an array of all the items in this line 

    # Skip blank lines, and comment lines that start with the hash symbol 
    if len(cells) == 0 or cells[0] == '#': 
     continue 

    # Skip lines that end with * (lets us avoid the column titles line) 
    if cells[len(cells)-1] == "*": 
     continue 

    # Add a row to the matrix, in C++ it would be something like mat.push_back(vector<int>()) 
    mat.append([]) 

    # Add all the items except the first one as numbers 
    for i in range(1, len(cells)): # Equivalent to C++'s "for (int i = 1; i < cells.size; ++i) { }" 
     mat[len(mat)-1].append(int(cells[i])) 

    fh.close() 
    return mat 

mat = readfile(sys.argv[1]) 
print "Cell at 3,4 is %u" % mat[3][4] 
2

有幾個問題。正如已經提到的,您從 返回從嵌套循環的中間:當然不是你想要做什麼 。此外,在循環之前,你讀入 矩陣[線] [COLS]:這是不確定的行爲,因爲你已經 從未初始化線和cols。 (通常情況下,甚至不 定義它們,直到爲例如: 對(INT行= 0;線< 30; ++線) 對(INT COLS = 0; COLS < 30; ++ COLS) )但是,該行在語法上是有效的,並從輸入中讀取第一個非空白字符(在您的情況下爲最初的#)。 寫在某個地方,雖然它是任何人猜測的地方。

一些額外的意見: - readfile的參數應該是char const *,而不是 char *。甚至std :: string const &。

- 我不知道你想讀入矩陣什麼: 哪裏做30來自於它的定義是什麼?它不應該是 它是「int matrix [x] [x]」,或者沿着這些線。 或更可能:「std :: vector>」。你正在讀整數(或者寫成,單個字符); 精度沒有影響。它對輸入也沒有影響。

- 什麼是「ifs.setf(ios :: fixed,ios :: showpoint)」應該做 。 (碰巧,它幾乎肯定將浮點格式設置爲其默認值---而不是固定的---, ,儘管我認爲該行爲在形式上是未定義的。)非 重要;這些標誌對輸入也沒有影響。 「while(!ifs.eof())」也肯定是錯誤的。 012zz表達式ifs.eof()僅在輸入 操作失敗。

此外,您需要對第一個 非註釋行進行一些特殊處理,並對每行中的第一個 字符進行一些特殊處理。

- James Kanze