Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there no dscanf()?

Tags:

c

linux

posix

scanf

Why is there no dscanf function for reading from file descriptors?

We have fprintf, sprintf and dprintf for printing but for scanning there is only fscanf and sscanf.

like image 794
rondoisthebest Avatar asked Dec 08 '15 06:12

rondoisthebest


2 Answers

It is not possible to write dscanf.

The scanf family requires that the stream is buffered, or else that it is capable of putting back a character that was read. POSIX file descriptors are neither.

Consider this stream

1234xyz

How would you scan a number off it? You cannot read it byte by byte and stop just before x. That would require clairvoyance. You also cannot read x and decide you don't need it, because you cannot put it back.

like image 102
n. 1.8e9-where's-my-share m. Avatar answered Nov 10 '22 09:11

n. 1.8e9-where's-my-share m.


Because the printf and scanf family of functions is part of the C language, the functions handling file descriptors are not. Instead they are part of the operating system API (on POSIX platforms like Linux or OSX, other platforms emulate these system calls).

And the dprintf function is not a standard C function, it's an extension. From this printf (and family) manual page:

The dprintf() and vdprintf() functions were originally GNU extensions that were later standardized in POSIX.1-2008.

That there's no dscanf function is probably just an oversight from those who made the original dprintf extension.

like image 37
Some programmer dude Avatar answered Nov 10 '22 10:11

Some programmer dude