【问题标题】:How to fold input when max character limit is reached?达到最大字符限制时如何折叠输入?
【发布时间】:2020-05-26 19:54:19
【问题描述】:

我正在解决 C ansi 编程书中的一个问题。我要求用户输入,直到没有行尾。但是,一旦达到 10 个字符,我希望将字符移到下一行。但是,换行符仅在按 Enter 后才起作用。一旦 i == 10 不应该输出一个新行吗?

#include <stdio.h>
#define MAXLINE 10


// count number of chars, once it reaches certain amount

int main() 
{
    int i,c;

    for (i=0;(c=getchar()) != EOF; i++)
    {
        if (c == '\n'){
            i = 0;
        }

        else if (i == MAXLINE){
            printf("\n");
        }

    }
    //printf("%d\n",i);

}

谢谢。

【问题讨论】:

标签: c algorithm input io


【解决方案1】:

不应该输出一次换行符i == 10吗?

没有。因为控制台输入默认是缓冲的。 getchar() 在找到stdin 中的换行符'\n' 之前不会返回stdin 中的下一个字符。刷新缓冲区需要换行符。

有一些基于实现的解决方案可以立即刷新输入,而不是等待换行符。例如 Windows/DOS 下 conio.h 中的 getche()cbreak() 选项,并在 Linux 的 curses-library 中使用 getch() 而不是 getchar()

您的计数也不正确,i = 0;if (i == MAXLINE) 在 11 个字符之后将在输出中放置一个换行符,而不是在 10 个字符之后。这是因为您从 0 开始,而不是 1。请改用i = 1if (i == (MAXLINE - 1))


如果您使用的是 Windows/DOS,请尝试:

#include <stdio.h>
#include <conio.h>             // Necessary to use getche().

#define MAXLINE 10


// count number of chars, once it reaches certain amount

int main (void) 
{
    int i, c;

    for (i = 0; (c = getche()) != EOF; i++)
    {
         if (i == (MAXLINE - 1))
         {
             printf("\n");             
             i = -1;          // Counter is reset. To break out of the loop use CTRL + Z.
         }
    }

    //printf("%d\n",i);
}

如果计数器重置对你来说有点难以理解,上面的代码基本上相当于:

#include <stdio.h>
#include <conio.h>             // Necessary to use getche().

#define MAXLINE 10


// count number of chars, once it reaches certain amount

int main (void) 
{
    int i, c;

    for (i = 1; (c = getche()) != EOF; i++)
    {
         if (i == MAXLINE)
         {
             printf("\n");
             i = 0;          // Counter is reset. To break out of the loop use CTRL + Z.
         }
    }

    //printf("%d\n",i);
}

对于 Linux,请使用 ncurses 库中的 cbreak()getch()

#include <stdio.h>
#include <ncurses.h>            

#define MAXLINE 10


// count number of chars, once it reaches certain amount
int main (void) 
{
    cbreak();
    echo();

    initscr();

    int i, c;

    for (i = 1; (c = getch()) != ERR; i++)
    {
         if (i == MAXLINE)
         {
             printf("\n");
             refresh();
             i = 0;          // Counter is reset. To break out of the loop use CTRL + D.
         }
    }

    //printf("%d\n",i);

    endwin();
}

注意:要使用 ncurses 库,您需要在调用编译器时添加 -lnurses 选项。

此外,您需要使用initscr()endwin() 来打开和关闭curses 终端窗口。

【讨论】:

  • 我将如何使用这个解决方案?这个网站fresh2refresh.com/c-programming/c-file-handling/… 只是说 getche() 将回显字符输入。这不是已经发生了吗?
  • @ColorfulCodes 不,不是。这就是我所说的。如果您使用 Windows,请使用 #include &lt;conio.h&gt; 并将 getchar() 替换为 getche()
  • @ColorfulCodes 也许我对您之前的评论有误会。是的,您实际上可以看到您使用getchar() 在控制台中输入的内容,但它不会在程序本身中使用,直到在stdin 中找到换行符 - 通过按下 [Enter]/[Return] f.e. .getche() 将两者结合起来,回显输入但也立即返回字符。
  • @ColorfulCodes 还用一个例子更新了我的答案。如果您想在每 10 个字符后打印一个换行符,我忘了提到您的计数器不正确。
  • 实际上,此代码对我不起作用,但我会保留您的答案,因为它仍然为我提供了新信息和其他地方可以查看。这在 Linux 上不起作用。在安装后编译代码时,我也尝试过使用 -lcurses 的 ncurses.h 和 curses.h。我很感激帮助。希望我的评论能帮助其他 Linux 用户。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多