达到最大字符数限制时如何折叠输入? [英] How to fold input when max character limit is reached?

查看:112
本文介绍了达到最大字符数限制时如何折叠输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working through a problem in the C ansi programming book. I am asking the user for input until there is no end of line. However, i'd like to have the characters moved to the next line once 10 characters have been reached. However, the newline character only works after hitting enter. Shouldn't a new line be outputted once 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);

}

谢谢。

推荐答案


一旦 i == 10

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

No. Because the console input is buffered by default. getchar() will not return the next character in stdin before it found a newline character '\n' in stdin. The newline is required to flush the buffer.

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

There are implementation-based solutions possible to flush the input immediately and not waiting for the newline. For example getche() in conio.h under Windows/DOS or the cbreak() option and using getch() instead of getchar() in the curses-library for Linux.

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

Also your counting is incorrect, with i = 0; and if (i == MAXLINE) after 11 characters will a newline be placed in the output, not after 10. This is because you start at 0, not 1. Use either i = 1 or if (i == (MAXLINE - 1)) instead.

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

If you are on Windows/DOS, try:

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

如果您很难重置计数器,上面的代码基本上等同于:

If the counter reset is a bit hard to understand for you, the code above is basically equivalent to:

#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库的code> 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-library,您需要添加 -inurses 选项调用编译器。

Note: To use the ncurses-library, you need to add the -lnurses option at invoking the compiler.

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

Furthermore, you need to use initscr() and endwin() to open and close the curses terminal window.

这篇关于达到最大字符数限制时如何折叠输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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