Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 当达到最大字符限制时,如何折叠输入?_C_Algorithm_Input_Io - Fatal编程技术网

C 当达到最大字符限制时,如何折叠输入?

C 当达到最大字符限制时,如何折叠输入?,c,algorithm,input,io,C,Algorithm,Input,Io,我正在解决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=

我正在解决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);

}
#包括
#定义MAXLINE 10
//一旦达到一定数量,计算字符数
int main()
{
int i,c;
对于(i=0;(c=getchar())!=EOF;i++)
{
如果(c=='\n'){
i=0;
}
else if(i==MAXLINE){
printf(“\n”);
}
}
//printf(“%d\n”,i);
}
多谢各位

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

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

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

此外,您的计数不正确,
i=0
如果(i==MAXLINE)
在11个字符之后,将在输出中放置换行符,而不是在10个字符之后。这是因为您从
0
开始,而不是从
1
开始。改用
i=1
if(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);
}
注意:要使用ncurses库,需要在调用编译器时添加
-lnurses
选项


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

输入不会刷新到输入流中,直到您仍然按enter键。它是行缓冲的,甚至是完全缓冲的。我将如何使用此解决方案?这个站点只是说getche()将回显字符输入。这不是已经发生了吗?@ColorfulCodes不,不是。我就是这么说的。如果您使用Windows,
#包括
并用
getche()
@colorfolcodes替换
getchar()
。是的,您实际上可以通过
getchar()
看到您在控制台中输入的内容,但是在
stdin
中找到换行符之前,它不会在程序本身中使用-通过按[Enter]/[Return]f.e.
getche()
组合这两个选项,回显输入,但也立即返回字符。@ColorfulCodes还用一个示例更新了我的答案。我忘了提一下,如果你想在每10个字符后打印一行换行符,那么你的计数器是不正确的。实际上,这个代码对我不起作用,但我会检查你的答案,因为它仍然给了我新的信息和其他地方可以查看。这在Linux上不起作用。我尝试了ncurses.h和curses.h,在安装后编译代码时也使用了-lcurses。我感谢你的帮助。希望我的评论能帮助其他Linux用户。
#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);
}
#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();
}