sscanf在未知大小的矩阵上的用法? [英] sscanf usage on matrix of unknown size?

查看:43
本文介绍了sscanf在未知大小的矩阵上的用法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有一个包含NxM大小矩阵的文件.例如:

So, I have a file that contains a matrix of NxM size. For example:

P2
3 3 1
1 0 0
0 1 0
0 0 1

'P2'只是一个无用的指示符,第一个'3'指示有多少列,第二个'3'指示有多少行,'1'指示矩阵编号中的最大值.该矩阵存储在这样的数据结构中:

The 'P2' is just an useless indicator, the first '3' indicates how many columns are and the second '3' indicates how many lines, '1' indicates the maximum value among the matrix's numbers. This matrix was stored in a data structure like this:

typedef struct {
    int c; // columns
    int l; // lines
    unsigned char max; // max value
    unsigned char** data // variable to store matrix's numbers
} Matrix;

为了将文件中的数字存储到data变量中,我使用了fread函数,如下所示:

To store the numbers from file to the data variable, I used the fread function, like this:

Matrix* newMatrix = NULL;
newMatrix = malloc(sizeof(Matrix));

FILE* fp = NULL;
fp = fopen(matrixfile, "r");

long size;
fseek(matrixfile, 0, SEEK_END);
size = ftell(matrixfile);
newMatrix->data = malloc(size);

// Jump the 'P2' bytes.
fseek(matrixfile, 2, SEEK_SET);

// Get the c, l and max values.
fscanf(matrixfile, "%i %i %i", &newMatrix->c, &newMatrix->l, &newMatrix->max);

// Jump a '\n' character.
fseek(matrixfile, 1, SEEK_CUR);

// Get matrix's numbers.
fread(newMatrix->data, 1, size, matrixfile);

好的,我将矩阵的数字作为字符串存储在'unsigned char ** data'变量中.但是现在我需要使用这些数字,因此我正在尝试将此字符串转换为整数矩阵.我试图做这样的事情:

Ok, I have the matrix's numbers stored as a string in the 'unsigned char** data' variable. But now I need to work with those numbers, so I'm trying to transform this string into a matrix of integers. I tried to do something like this:

void StringtoInt (Matrix* str){

int matrixAux[str->l][str->c], i, j;
for(i=0; i<str->l; i++)
    for(j=0; j<str->c; j++)
        sscanf(str->data, "%i ", &matrixAux[i][j]);
}

好吧,我知道为什么这行不通,为什么我的"matrixAux"将是只有1的CxL矩阵.但我不知道在不知道矩阵中有多少个元素的情况下使用sscanf的任何方法.

Well, I understand why this doesn't work and why my 'matrixAux' will be a CxL matrix with only 1's. But I cannot think of any way to work with sscanf without knowing how many elements are in the matrix.

所以,我的问题是:有没有更好的方法可以将'unsigned char ** data'字符串转换为整数矩阵,而无需更改'data'类型(unsigned char **)?

So, my question: is there a better way to transform the 'unsigned char** data' string into a integer matrix WITHOUT changing the 'data' type (unsigned char**)?

我认为也许我只是在使用错误的方法将文件的矩阵存储到数据变量(fread函数)中,或者弄乱了指向指针语法的指针.但是我也看不到有其他好的替代方法可以做到这一点.

I think maybe I'm just using the wrong method to store the file's matrix into the data variable (fread function), or messing up with the pointer to pointer syntax. But I also don't see any other good alternative to do that.

推荐答案

问题1:计算数据

Problem 1: Computing the size of data

如果矩阵存储为文本文件,就像您使用

If the matrix is stored as a text file, just like you have posted, using

fseek(matrixfile, 0, SEEK_END);
size = ftell(matrixfile);

提出 data 的大小是不正确的.

to come up with the size of data is not correct.

您要做的就是读取行数和列数,然后可以使用 numRows * numCols 得出 data 的大小

All you have to do is read the number of rows and the number of columns and then, you can use numRows * numCols to come up with the size of data.

问题2:为数据

Problem 2: Allocating memory for data

使用

newMatrix->data = malloc(size);

数据分配内存似乎表明缺乏对内存分配方式的了解.

to allocate memory for data seems to indicate a lack of understanding of how memory is allocated.

数据的类型为 char ** .

malloc(size)为大小为 size 的字符数组分配内存.将 malloc(size)的返回值分配给 newMatrix-> data 是错误的.

malloc(size) allocates memory for an array of characters of size size. Assigning the return value of malloc(size) to newMatrix->data is wrong.

您需要的是:

newMatrix->data = malloc(numRows*sizeof(char*)); // Assuming you read numRows first.
for ( int i = 0; < numRows; ++i )
{
   newMatrix->data[i] = malloc(numCols);
}

读取数据

现在您可以使用以下方法从文件中读取数据:

Now you can read the data from the file using:

for ( int i = 0; < numRows; ++i )
{
   for ( int j = 0; j < numCols; ++j )
   {
      int number;
      if ( fscanf(matrixfile, "%d", &number) == 1 )
      {
         // Check that number is within range.
         // ...

         newMatrix->data[i][j] = number;
      }
      else
      {
         // Unable to read the number.
         // Deal with the error.
         exit(1);
      }
   }
}

这篇关于sscanf在未知大小的矩阵上的用法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆