-1

我有这个防御性编程问题,我真的不知道如何解决。

我有这个函数,它将文件路径和表的大小(行/列计数)作为参数,我正在寻找更好的方法来验证输入文件。我假设这个函数的参数总是正确的。size表示存储在文件中的表的“较小的一面”:

例如 :

1 2 3 4 
5 6 7 8 

size = 2 是正确的,而

1 2 3 4 5
5 6 7 8 9

size = 2 不正确

我也希望能够拒绝这样的文件

1 2 3 4 5 6 7 8

size = 2(通过 fscanf 接受)

我希望能够拒绝的另一种文件类型是

1 2 3
4 5 6

大小 = 2

至于现在我唯一的安全措施是检查文件的元素是否真的是数字。

这是我到目前为止所做的代码:

void import(float** table, int size, char* path)
{
    FILE* data = fopen(path, "r");
    assert(data);
    int i,j;
    int st;

    for (i=0; i<size; i++)
    {
        for(j=0; j<(size*2)-1; j++)
        {
            st = fscanf(data, "%f", &table[i][j]);
            if (!st)
            {
                printf("Error while importing the file.\n");
                fclose(data);
                return -1;
            }
        }
    }
    fclose(data);
}

我真的不知道从哪里开始以及如何开始,我并不是很精通 C,似乎存在很多功能和机制来做我想做的事情,但它们看起来都非常复杂,有些实际上比我的代码长假如。

如果有人能指出我正确的方向,那就太好了。

4

3 回答 3

1

您的 for 循环可能如下所示:

char line[1000], *token;
for (i = 0; i < size; i++) // for each line
{
    if (fgets(line, 1000, data) != NULL) // read line
    {
        token = strtok (line," ");
        for (j = 0; j < (size * 2) - 1; j++) // for each number from line
        {
            if (sscanf(token, "%f", &table[i][j]) <= 0)
            {
                // there are columns missing:
                printf("Error while importing the file.\n");
                fclose(data);
                return -1;
            }
            token = strtok (NULL," ");
        }
    }
    else
    {
        // there are rows missing:
        printf("Error while importing the file.\n");
        fclose(data);
        return -1;
    }
}

另请注意,assert(data);应将其替换为以下内容:

if (!data)
{
    printf("Error while openning the file [filePath=\"%s\"].\n", filePath);
    cleanExit();
}
于 2012-02-20T21:38:19.843 回答
1

您无法轻易检测 中的行尾scanf(),因此直接使用它不会满足您的标准。

您可能需要阅读整行(fgets()或者可能getline()),然后依次处理每一行。行处理可以使用sscanf(),也可以使用%n指令。概括地说,这归结为:

for (line_num = 0; line_num < size; line_num++)
{
    ...read line from file into buffer line, checking for EOF...
    start = line;
    for (i = 0; i < 2 * size; i++)
    {
        if (sscanf(start, "%f%n", &value, &offset) != 1)
            ...ooops - short line or non-numeric data...
        else
        {
            start += offset;
            table[line_num][i] = value;
        }
    }
}
...check that there's no clutter after the last expected line...
于 2012-02-20T21:21:06.333 回答
0

您还可以计算整个文件的校验和。问题是你对此有多认真。创建一个异或校验和很容易,但它对碰撞并不安全。如果它很重要,最好的可能是使用类似 sha-1 的东西。

于 2012-02-20T21:36:22.010 回答