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

使用printf(c-linux)后如何将光标对准左侧,c,linux,system-calls,C,Linux,System Calls,我使用的是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

我使用的是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)

在输出时将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
可能不会像以前那样重置所有内容

在外壳上运行:

$ 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;
                      $ $ $
$

奇怪的环境,但它可能需要
\r\n
而不是
\n
?@yunnosch ya这只是一个简单的例子。我有办法摆脱循环xD,你是对的。你介意回答一下吗?我可以选择\r\n成功了!仔细阅读整页。它既长又神秘,但如果你想使用原始模式,你需要了解所有内容。或者,切换到使用。@zwol我会将你的评论输入添加到我的答案中,以使其在复杂性和灵活性范围内更加平衡。如果你不介意,或者单独回答。
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;