繁体   English   中英

代码没有打印预期的输出,为什么?

[英]The Code doesn't print the expected output, why?

以下代码无法正常运行..

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

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


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[])
{
    // declaring variables
    float lon, lat;
    char info[80];
    FILE *reader;

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

    // opening the reader
    if (!(reader = fopen(argv[1], "r"))) {
        fprintf(stderr, "File can't be accessed: %s\n", argv[1]);
        return 2;
    }

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

    struct dest other;
    strcpy(other.filename, argv[2]);
    other.opened = false;

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

    // finally, scanning the file ..
    struct dest *use_f; // pointer for the wanted destination ..
    bool known;
    while (fscanf(reader, "%f,%f,%79[^\n]", &lat, &lon, info)) {

        // deciding which file to use ..
        known = false;
        for(short i=0; i < pairs; ++i) {
            if (strstr(info, data[i].keyword)) {
                known = true;
                use_f = &data[i];
            }
        }

        if (!(known)) {
            use_f = &other;
        }

        // checking the file ..
        if (!((*use_f).opened)) {
            (*use_f).file = fopen((*use_f).filename, "w");
            (*use_f).opened = true;
        }

        // writing to the file ..
        fprintf((*use_f).file, "%f,%f,%s\n", lat, lon, info);
    }

    // closing all data streams, and informing user ..
    for (short i=0; i < pairs; ++i) {
        display_data(data[i]);
        if (data[i].opened) {
            fclose(data[i].file);
            data[i].opened = false;
        }
    }

    fclose(reader);
    fclose(other.file);

    return 0;
}

运行它的命令是这个..

./归类为spooky.csv other.csv UFO UFOS.csv#我根本没有输出

看来while循环实际上并没有结束,这很神秘,因为文件(spooky.csv)只有11行!

30.685163,-68.137207,Type = Yeti
28.304380,-74.575195,Type = UFO
29.132971,-71.136475,Type =船
28.343065,-62.753906,Type =猫王
27.868217,-68.005371,Type = Goatsucker
30.496017,-73.333740,Type =消失
26.224447,-71.477051,类型= UFO
29.401320,-66.027832,Type =船
37.879536,-69.477539,Type =猫王
22.705256,-68.192139,Type =猫王
27.166695,-87.484131,Type =猫王

它只是不断写入other.file,但我不知道为什么..
该程序只是没有结束,有人可以向我解释吗?

fscanf()联机帮助页:“如果在进行任何转换(例如文件结束)之前发生输入失败,则返回EOF值。”

这是一个提示... EOF不等于0。while循环永远不会终止。

暂无
暂无

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

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