在 C 中反转 stdin [英] Reversing stdin in C

查看:22
本文介绍了在 C 中反转 stdin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些关于如何反转 stdin 内容的建议.这是我处理反转标准输入的代码部分:

I need some advice on how to reverse the contents of stdin. This is the part of my code which handles reversing stdin:

int reversestdin(FILE *file)
{
    int a, b;
    int lineas=0;
    while ((a=fgetc(stdin)) !=EOF)
    {
        if (a=='\n')
            lineas++;
    }
    for(b=0; b<lineas; b++)
    { 
        rewind(stdin);
        int d,len, e;
        char str[2048], *ptr;
        for(e=0; e<=b; e++)
        { 
            fgets(str, 2048, stdin);
        }
        ptr=str;
        for(d=0; d<2048; d++)
        {
            if(*ptr=='\n')  break;
            if(*ptr=='\0')  break;
            ptr++;
        }
        len=d;
        ptr--;
        for(d=len; d>0; d--)
        {
            printf("%c", *ptr--);
        }

        printf("\n");
    }
    return 0;
}

我还有一个名为 example1 的 .txt 文件,其内容如下:

I also have a .txt file called example1 with the following contents:

LINE 1.1
LINE 1.2

LINE 1.4

当我执行 ./myprogram < 时,此代码有效示例1.txt.它输出

1.1 ENIL
2.1 ENIL

4.1 ENIL

但是,如果我执行 echo "This text should be reversed" |./myprogram 输出:

But, if I execute echo "This text should be reversed" | ./myprogram it outputs:

(

就是这样.一个开放的括号.我发现如果我省略了计算行数的代码部分,而只是手动说有 1 行它可以工作(当然是 1 行).

That's it. An open bracket. I've discovered that if I omit the part of my code which counts lines and just manually say there is 1 line it works (for 1 line of course).

int reversestdin(FILE *file)
{
    int a, b;
    int lineas=1;
    //while ((a=fgetc(stdin)) !=EOF)
    //{
        //if (a=='\n')
            //lineas++;
    //}
    for(b=0; b<lineas; b++)
    { 
        rewind(stdin);
        int d,len, e;
        char str[2048], *ptr;
        for(e=0; e<=b; e++)
        { 
            fgets(str, 2048, stdin);
        }
        ptr=str;
        for(d=0; d<2048; d++)
        {
            if(*ptr=='\n')  break;
            if(*ptr=='\0')  break;
            ptr++;
        }
        len=d;
        ptr--;
        for(d=len; d>0; d--)
        {
            printf("%c", *ptr--);
        }

        printf("\n");
    }
    return 0;
}

现在输出

desrever eb dluohs txet sihT

推荐答案

首先,不支持回滚 stdin,除非(理论上)它是从输入文件重定向的.既然没有办法(我知道,无论如何)知道已经发生了,不要费心去尝试.

First, rewinding stdin isn't supported unless (in theory) it has been redirected from an input file. And since there is no way (I know of, anyway) of knowing that has happened, don't bother trying it.

也就是说,与明显流行的观点相反,您不必缓冲整个输入.您仍然可以一次一行地缓冲它:

That said, contrary to what is apparently popular opinion, you do not have to buffer the entire input. You can still buffer it line-at-a-time:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int buffer[2048], ch, lines=0, i=0;
    do
    {
        ch = fgetc(stdin);
        if ((ch == '\n') || 
            (ch == EOF && i > 0) || 
            (i == (sizeof(buffer)/sizeof(*buffer)-1)))
        {
            ++lines;
            while (i != 0)
                fputc(buffer[--i], stdout);
            fputc('\n', stdout);
        }
        else
        {
            buffer[i++] = ch;
        }
    } while (ch != EOF);

    return 0;
}

很确定这会满足您的要求(或接近它,无论如何)

Pretty sure that will do what you're looking for (or close to it, anyway)

这篇关于在 C 中反转 stdin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆