使用printf后如何让光标左对齐(clinux)

How to align the cursor to the left side after using printf (c linux)

我正在使用 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
                        **

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

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

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

onlcr (-onlcr)
Map (do not map) NL to CR-NL on output.

如果您希望保持不变,请相应地调整您的 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(和return)后,我输入了stty -acontrol-J 以获得缩进输出。 3 $ 提示包括我输入 stty "$old" control-J 和点击 return。稍微重新格式化后,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;