简体   繁体   English

以下代码不起作用..为什么?

[英]The following code doesn't work .. why?

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 ./归类为spooky.csv other.csv UFO UFOS.csv
Keyword: ?, Filename: @, Used: No 关键字:?,文件名: @,使用:否

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) 第二个参数 :程序应该在spooky.csv文件中找到的任何未知信息处存储的文件 (在此实现中也被忽略)
The other arguments : they are parsed as pairs, the first is the keyword, the second is the file .. 其他参数 :它们被解析为对,第一个是关键字,第二个是文件..

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 .. 任何帮助将不胜感激..

You have 2 struct dest data[] arrays. 您有2个struct dest data[]数组。 The inner one is masking the outer - get rid of it. 内在的是遮蔽外在的-摆脱它。

Your compiler is probably warning about this, if you have warnings turned on. 如果您打开了警告,则编译器可能会对此发出警告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM