简体   繁体   English

从标准输入读取输入直到空行

[英]Reading input from stdin until empty line

I want to parse input such as: 我想解析输入,例如:

1 2 3\n4 5 6\n7 8 9\n\n

and for each row save each value in int ant print it to stdout until I get an empty line, so for this example I would get: 并为每行将每个值保存在int ant中,将其打印到stdout,直到我得到一个空行,因此对于此示例,我将得到:

1 2 3
1 2 3
4 5 6
4 5 6
7 8 9
7 8 9

I've tried something like 我已经尝试过类似

int n1, n2, n3;
while(scanf ("%d %d %d\n", n1, n2, n3) != EOF) {
    printf("%d %d %d\n", n1, n2, n3);
    fflush(stdout);
}

but it doesn't seem to work. 但它似乎不起作用。 Is there any simple way to do that? 有没有简单的方法可以做到这一点?

scanf cannot achieve what you are trying to do because it keeps reading until the condition is met, and %d specifier will ignore '\\n' newline character, I propose this code scanf无法实现您要执行的操作,因为它会一直读取直到满足条件为止,并且%d指定符将忽略'\\n'换行符,我建议使用此代码

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

int  main()
{
    int n1, n2, n3;
    char line[64];

    /* read at least 63 characters or unitl newline charater is encountered with */
    /*    fgets(line, sizeof line, stdin) */
    /* if the first character is a newline, then it's an empty line of input */
    while ((fgets(line, sizeof line, stdin) != NULL) && (line[0] != '\n'))
    {
        /* parse the read line with sscanf */
        if (sscanf(line, "%d%d%d", &n1, &n2, &n3) == 3)
        {
            printf("%d %d %d\n", n1, n2, n3);
            fflush(stdout);
        }
    }
    return 0;
}

While this code works it's not robust, since it will fail in the case commented below by WhozCraig , so this is a way to do it that will keep you safe from the problem 尽管此代码有效,但它并不健壮,因为在以下WhozCraig注释的情况下它将失败,因此这是一种使您免受问题困扰的方法

#include <stdio.h>
#include <string.h>
#include <ctype.h> /* for isspace */

int isEmpty(const char *line)
{
    /* check if the string consists only of spaces. */
    while (*line != '\0')
    {
        if (isspace(*line) == 0)
            return 0;
        line++;
    }
    return 1;
}

int  main()
{
    int n1, n2, n3;
    char line[64];

    while ((fgets(line, sizeof line, stdin) != NULL) && (isEmpty(line) == 0))
    {
        if (sscanf(line, "%d%d%d", &n1, &n2, &n3) == 3)
        {
            printf("%d %d %d\n", n1, n2, n3);
            fflush(stdout);
        }
    }
    return 0;
}

I didn't really like any of the answers provided since they don't also capture the input. 我真的不喜欢提供的任何答案,因为它们也无法捕获输入内容。 I needed to capture the entire read contents and process them in batch. 我需要捕获整个读取的内容并进行批量处理。 So here's what I came up with: 所以这是我想出的:

char * readFromFileUntilBlankLine(FILE *in) {
    int growBy = 1000;
    int allocatedSize = growBy;
    char *buffer = malloc(allocatedSize * sizeof(char));
    strcpy(buffer, "");

    char * line = NULL;
    size_t len = 0;
    ssize_t read = 0;

    while ((read = getline(&line, &len, in)) != EOF) {
        // Are we done? Is this blank?
        if (line[0] == '\n') {
            free(line);
            return buffer;
        }

        // Ensure there is enough space in buffer for line
        int newSize = strlen(buffer) + read + 1;
        if (newSize > allocatedSize) {
            allocatedSize = newSize + growBy;
            buffer = realloc(buffer, allocatedSize * sizeof(char));
        }
        strcat(buffer, line);

        free(line);
        line = NULL;
    }

    if (line) {
        free(line);
    }

    return buffer;
}

This suffers from the same "\\n \\n" problem discussed in a previous response, but was sufficient for my needs. 这也遇到了先前答复中讨论的"\\n \\n"问题,但足以满足我的需求。

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

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