【问题标题】:The Code doesn't print the expected output, why?代码没有打印预期的输出,为什么?
【发布时间】:2014-07-16 21:15:16
【问题描述】:

以下代码的行为与预期不符..

#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;
}

用来运行它的命令是这个..

./categorize 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,Type=UFO
29.401320,-66.027832,Type=船
37.879536,-69.477539,Type=猫王
22.705256,-68.192139,Type=猫王
27.166695,-87.484131,Type=猫王

它只是不断写入 other.file,但我不知道为什么..
程序根本就没有结束,谁能给我解释一下?

【问题讨论】:

  • @redFIVE 喜欢什么?我不知道有什么好的 c 调试器..
  • @AmrAyman 尝试检查fscanf 的返回值并打印出它读取的值。
  • 只是好奇,你为什么用short而不是int
  • @Jack 为了提高内存效率..
  • 为什么有这么多反对票? o.O 的问题有什么问题?

标签: c struct io stdin


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2011-01-23
    • 2019-11-14
    • 1970-01-01
    • 2021-03-05
    • 2022-08-18
    • 2017-05-17
    • 1970-01-01
    • 2018-04-30
    • 2018-08-08
    相关资源
    最近更新 更多