4 ms·
Keep string formatting separate from IO. The simple POSIX write/read works nicely enough for IO. Very versatile and damn simple. No "oop" necessary. For strin
by goalieca 11y ago
Keep string formatting separate from IO.
The simple POSIX write/read works nicely enough for IO. Very versatile and damn simple. No "oop" necessary.
For string parsing, people use all kinds of things from regex to deserializers. Sometimes we even use simple string tokenisation function. No one ever uses scanf or cin.
- jws 11y agoThe simple POSIX write/read works nicely enough for IO – where simple includes the loop to check the return code and then errno for cases that should be retried, ( EINTR, EAGAIN, EWOULDBLOCK, depending on what you are doing and your OS), adjust your arguments for how much data actually got sent or read, and then retry. Also be aware that depending on your file descriptor you might be making storms of tiny context switches, interrupts, and IOs. Old Unix programmers handle that with about as much thought as breathing, but only because they were young unix programmers once. Copying the bytes one more time isn't the worst thing that you will ever make a computer do. The stdio.h level is pretty nice from the standpoint of keeping your foot un-blown-off.
- stevedonovan 11y agoThat's true - you never see scanf outside beginner exercises. stdio is definitely hard to use safely for input. In this library, I explore using operator() for input, but error handling is challenging.