How to Read Just One Character from the Terminal (stdin)?

preview_player
Показать описание
---

How to Read Just One Character from Stdin? // This sounds like it should be super easy, but it can be trickier than beginners expect. This video will take you through some of the messy parts.

***

Welcome! I post videos that help you learn to program and become a more confident software developer. I cover beginner-to-advanced systems topics ranging from network programming, threads, processes, operating systems, embedded systems and others. My goal is to help you get under-the-hood and better understand how computers work and how you can use them to become stronger students and more capable professional developers.

About me: I'm a computer scientist, electrical engineer, researcher, and teacher. I specialize in embedded systems, mobile computing, sensor networks, and the Internet of Things. I teach systems and networking courses at Clemson University, where I also lead the PERSIST research lab.

More about me and what I do:

To Support the Channel:
+ like, subscribe, spread the word

Рекомендации по теме
Комментарии
Автор

My most recent assignment is creating a shell from scratch in C and that was exactly what I was looking for. Very helpful!

annsaiko
Автор

I come here to learn X but goes back learning X, Y, Z... and may be thats why I love coming back here.

tusharsnn
Автор

Tempting to make this into a library function, but then you'd have to remember to revert back to the old terminal settings when you're done or had an early exit.

SimGunther
Автор

Nice, concise, and clear as always. As far as I understand, the termios settings govern any type of character device, including USB. However, reading from/writing to USB may not be as "simple" as working with the terminal. It would be nice to see some examples regarding USB communication in your future videos.

antonioastorino
Автор

also consider that not all platforms or languages are created equal.

you can #include conio.h and then use getch() and putch() to get and put characters in an unbuffered manner to a console-terminal. eg DOS
EOF still terminates the input.

at one point putchar() and getchar() did unbuffered input and output, but only to stdio

remember your C compiler guide is a wonderful source of resources for which functions do what and when they do it. Man pages as well. and all still trying Google your question. sometimes you need to do something on older hardware with an older compiler.

sometimes you just have to test some things.

Wyldone
Автор

It's exactly what i am looking for. I'm creating a simple game for the terminal for educational purpose.
Thank you Jacob!

Vulto
Автор

Might be a nice idea to do a video on NCurses and show a less platform dependent way of doing it. Or possibly SDL as it can handle keyboard input and might lead to some interesting stuff.

anon_y_mousse
Автор

Really well described, I totally got it, thank you JS

Recovered
Автор

In case of an early termination occurring before setting the settings back to the old settings due to a seg fault or a kill signal. Would the new settings still be used in the terminal?
Or does it use something like atexit to keep it from happening, in which case, wouldn't it be unnecessary to switch them back to the old settings?

bkn
Автор

Next question is how do we do a nonblocking get, that is a "return a character or nothing" instead of "return a character or wait"

boristheengineer
Автор

Can we make it simple by using fflush(stdin)?

rajaroychowdhury
Автор

Really great content, thank you!
Maybe add "unix/linux terminal driver buffering" to the title? After all it's the culprit :-)
Also in this mode (raw) all special keys such as enter and backspace lose their function.

beardymonger
Автор

3:15 How do you get your terminal prompt to look like that? Are you using oh my zsh?

joseville
Автор

check passwd program in busybox it hides the keyboard inputs...

anandmoon
Автор

This also gets a lot more interesting when the "characters" you read do not happen to be ASCII characters, but are UTF-8 characters encoded in more than one C char.

hundim
Автор

I think it would have been more clear if you said you where looking for "Non blocking" Keyboard input.
There used to be an input with Borlands that was non blocking. It wasn't part of the standard though.
You would have thought that someone like that would have been made into the standard.

diconicabastion
Автор

What about non-POSIX platforms? Is it possible to obtain a similar result with standard C?

Tachi
Автор

6:02, I assume that's a linux specific header, because I'm sure I remember windows using something like console.h (don't think that was the exact name but something along that line)

zxuiji
Автор

it is amazing, however the fact that it is so deeply hidden....

zyghom
Автор

Wait, but doesn't getc use buffering, like fread? If so, why does getc return immediately and not after the internal input buffer is filled? Does it know that the canonical mode was switched off?

bamberghh