下面的代码不起作用。为什么?

时间:2022-02-21 07:21:29

The following code isn't working as expected ..

下面的代码不像预期的那样工作。

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>

struct dest
{
    char filename[20], keyword[20];
    bool opened;
    FILE * stream;
};


void display_data(const struct dest p) {
    printf("Keyword: %s, Filename: %s, Used: %s\n", p.keyword, p.filename, p.opened ? "Yes" : "No");
}

int main(int argc, char const *argv[])
{
    // float lon, lat;
    // char info[80];

    if ((argc+1) % 2) {
        fprintf(stderr, "Usage: %s file_to_read file_for_unknown type file type file ...\n", argv[0]);
        return 2;
    }

    if (access(argv[1], F_OK) == -1) {
        fprintf(stderr, "File can't be accessed: %s\n", argv[1]);
        return 2;
    }

    const short pairs = (argc-3)/2;
    struct dest data[pairs];

    short times = 4;
    for(short i = 4; i < argc; i += 2) {
        struct dest data[i-times];
        data[i-times].opened = false;
        strcpy(data[i-times].keyword, argv[i-1]);
        strcpy(data[i-times].filename, argv[i]);
        // display_data(data[i-times]);
        times += 1;
    }

    display_data(data[0]);

    return 0;
}

That's what happens when I try to run it ..

这就是我试着运行它时发生的事情。

./categorize spooky.csv other.csv UFO UFOS.csv
Keyword: ?, Filename: �@, Used: No

/分类毛骨悚然。csv。csv不明飞行物UFO。csv关键字:?,文件名:�@,使用:没有

Which isn't that meaningful ..
I have been trying to work out the solution .. in vein ..
I don't understand where the problem is ..

这不是很有意义。我一直在设法解决这个问题。在静脉. .我不明白哪里出了问题。

Arguments are parsed as follows:

论点分析如下:

The first argument: the file the program is supposed to read from (ignored for now)
The second argument: the file the program is supposed to store at any unknown info found in the spooky.csv file (also, ignored in this implementation)
The other arguments: they are parsed as pairs, the first is the keyword, the second is the file ..

第一个参数:程序应该读取的文件(暂时忽略)第二个参数:程序应该存储在spooky中任何未知信息的文件。csv文件(在此实现中也忽略了)其他参数:它们被解析成对,第一个是关键字,第二个是文件。

My Solution for this filtering problem was to create an array of structs, and within each struct I store the keyword, the filename and the file io stream (which i am ignoring, for now) ..

我对这个过滤问题的解决方案是创建一个结构体数组,在每个结构体中我都存储关键字、文件名和文件io流(目前我忽略了这个)。

Any help would be highly appreciated ..

如有任何帮助,我们将不胜感激。

1 个解决方案

#1


4  

You have 2 struct dest data[] arrays. The inner one is masking the outer - get rid of it.

有两个struct dest data[]数组。内在的是掩盖外在的-摆脱它。

Your compiler is probably warning about this, if you have warnings turned on.

如果打开了警告,编译器可能会对此发出警告。

#1


4  

You have 2 struct dest data[] arrays. The inner one is masking the outer - get rid of it.

有两个struct dest data[]数组。内在的是掩盖外在的-摆脱它。

Your compiler is probably warning about this, if you have warnings turned on.

如果打开了警告,编译器可能会对此发出警告。