简体   繁体   中英

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
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 file the program is supposed to read from (ignored for now) :程序应从中读取的文件(目前忽略)
: 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文件中找到的任何未知信息处存储的文件 (在此实现中也被忽略)
: 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) ..

Any help would be highly appreciated ..

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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