2011-01-28 92 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