简体   繁体   中英

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

I am using printf but for some reason the cursor starts below the end of the previous line.

system("/bin/stty raw");

    while(true){

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

    }

system ("/bin/stty cooked");

My output ends up looking like this.

Enter a value: 
kk
  kk
    kk
      kk
        kk
          kk
            kk
              kk
                kk
                  kk
                    kk
                      kk
                        **

There is at least one environment which needs \\r\\n instead of \\n in some situations.

(In case this should survive for a long time, when the comments disappear, let me mention the recommendation from comments (credits to zwol), to use ncurses.)

Using stty raw turns off some output mapping options, such as onlcr :

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

If you want that still set, adjust your stty call accordingly. Alternatively, now you know why Windows uses CRLF ( "\\r\\n" ) line endings (and so do many Internet protocols); the CR moves the cursor to the start of line and the LF (aka NL ) moves the cursor down one line in the current column.

There are advantages to using popen("echo \\"stty $(stty -g)\\"", "r") to read the current terminal settings. It will generate a string such as

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

(that happens to be what I get on a Mac), which you can then run to reset the mode exactly as it was before you set it to raw . Setting to cooked may not reset everything the way it was before.

Running at the 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;
                      $ $ $
$

After typing stty raw (and return ), I typed stty -a control-J to get the indented output. The 3 $ prompts included me typing stty "$old" control-J and hitting return . When reformatted a bit, the settings in raw mode are:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM