使用printf后如何将光标对准左侧(c linux)

问题描述 投票:2回答:2

我正在使用printf但由于某种原因,光标从上一行的末尾开始。

system("/bin/stty raw");

    while(true){

        char c = getchar();
        printf("%c\n", c);

    }

system ("/bin/stty cooked");

我的输出最终看起来像这样。

Enter a value: 
kk
  kk
    kk
      kk
        kk
          kk
            kk
              kk
                kk
                  kk
                    kk
                      kk
                        **
c linux system-calls
2个回答
3
投票

在某些情况下,至少有一个环境需要\r\n而不是\n

(如果这应该存在很长时间,当评论消失时,让我提一下评论的建议(对zwol的信用),使用ncurses。)


3
投票

使用stty raw会关闭一些输出映射选项,例如onlcr

onlcr (-onlcr) 在输出上映射(不映射)NL到CR-NL。

如果您仍想设置,请相应调整您的stty电话。或者,现在您知道为什么Windows使用CRLF"\r\n")行结尾(许多Internet协议也是如此); CR将光标移动到行的开头,LF(又名NL)将光标向下移动到当前列中的一行。

使用popen("echo \"stty $(stty -g)\"", "r")读取当前终端设置是有好处的。它会生成一个字符串,如

stty gfmt1:cflag=4b00:iflag=6b02:lflag=200005cb:oflag=3:discard=f:dsusp=19:eof=4:eol=ff:eol2=ff:erase=7f:intr=3:kill=18:lnext=16:min=1:quit=1c:reprint=12:start=11:status=14:stop=13:susp=1a:time=0:werase=17:ispeed=9600:ospeed=9600

(这恰好是我在Mac上获得的),然后您可以运行以完全重置模式,就像将其设置为raw之前一样。设置为cooked可能不会像以前那样重置所有内容。

在shell上运行:

$ old=$(stty -g)
$ stty -a
speed 9600 baud; 65 rows; 110 columns;
lflags: icanon isig iexten echo echoe -echok echoke -echonl echoctl
    -echoprt -altwerase -noflsh -tostop -flusho pendin -nokerninfo
    -extproc
iflags: -istrip icrnl -inlcr -igncr ixon -ixoff ixany imaxbel iutf8
    -ignbrk brkint -inpck -ignpar -parmrk
oflags: opost onlcr -oxtabs -onocr -onlret
cflags: cread cs8 -parenb -parodd hupcl -clocal -cstopb -crtscts -dsrflow
    -dtrflow -mdmbuf
cchars: discard = ^O; dsusp = ^Y; eof = ^D; eol = <undef>;
    eol2 = <undef>; erase = ^?; intr = ^C; kill = ^X; lnext = ^V;
    min = 1; quit = ^\; reprint = ^R; start = ^Q; status = ^T;
    stop = ^S; susp = ^Z; time = 0; werase = ^W;
$ stty raw
$ speed 9600 baud; 65 rows; 110 columns;
                                                 lflags: -icanon -isig -iexten -echo -echoe -echok echoke -echonl echoctl
            -echoprt -altwerase -noflsh -tostop -flusho -pendin -nokerninfo
                                                                                -extproc
                                                                                        iflags: -istrip -icrnl -inlcr -igncr -ixon -ixoff ixany -imaxbel iutf8
                                                    ignbrk -brkint -inpck -ignpar -parmrk
                                                                                             oflags: -opost onlcr -oxtabs -onocr -onlret
                          cflags: cread cs8 -parenb -parodd hupcl -clocal -cstopb -crtscts -dsrflow
                                                                                                    -dtrflow -mdmbuf
          cchars: discard = ^O; dsusp = ^Y; eof = ^D; eol = <undef>;
                                                                        eol2 = <undef>; erase = ^?; intr = ^C; kill = ^X; lnext = ^V;
                        min = 1; quit = ^\; reprint = ^R; start = ^Q; status = ^T;
                                                                                    stop = ^S; susp = ^Z; time = 0; werase = ^W;
                      $ $ $
$

键入stty raw(并返回)后,我键入stty -a control-J以获得缩进输出。 3 $提示包括我键入stty "$old"控制-J和击中返回。重新格式化时,raw模式中的设置为:

speed 9600 baud; 65 rows; 110 columns;
lflags: -icanon -isig -iexten -echo -echoe -echok echoke -echonl echoctl
        -echoprt -altwerase -noflsh -tostop -flusho -pendin -nokerninfo
        -extproc
iflags: -istrip -icrnl -inlcr -igncr -ixon -ixoff ixany -imaxbel iutf8
        ignbrk -brkint -inpck -ignpar -parmrk
oflags: -opost onlcr -oxtabs -onocr -onlret
cflags: cread cs8 -parenb -parodd hupcl -clocal -cstopb -crtscts -dsrflow
        -dtrflow -mdmbuf
cchars: discard = ^O; dsusp = ^Y; eof = ^D; eol = <undef>;
        eol2 = <undef>; erase = ^?; intr = ^C; kill = ^X; lnext = ^V;
        min = 1; quit = ^\; reprint = ^R; start = ^Q; status = ^T;
        stop = ^S; susp = ^Z; time = 0; werase = ^W;
© www.soinside.com 2019 - 2024. All rights reserved.