Home > Software engineering >  Abstraction Stream C
Abstraction Stream C

Time:09-22

How can a data stream work for a file on the hard drive and also for a printer? Is there an abstraction layer before the "Stream" that separates the devices with their respective properties?

CodePudding user response:

Is there an abstraction layer before the "Stream" that separates the devices with their respective properties?

You've tagged C and mentioned it in the question title, but this is not a characteristic of the C language or standard library. C provides for I/O via streams, but it does not speak to what kinds of devices are served that way.

The "everything is a file" paradigm is a characteristic of UNIX (which you've also tagged). That's where the abstraction resides. A program running in a hosted C implementation on UNIX can access different kinds of devices via streams because the operating system provides for it. It can also use the same set of POSIX low-level I/O functions for all those devices, because, again, the OS provides for it. This is effectively the realm of device drivers and the interfaces that the OS kernel requires them to provide.

CodePudding user response:

The stream is the abstraction (or at least part of the abstraction). The standard library provides you with a single, consistent interface for doing stream operations, regardless of whether the target is your console, a file on disk, a printer, or some other destination. You just call fopen, fscanf, fwrite, etc., and don't worry about the details.

The implementations of fopen, fread, fwrite, etc., make all the system-specific calls to necessary manipulate files or open I/O channels to other devices. And even those are usually higher-level abstractions provided by file systems and device drivers. Naturally, implementations on Windows make different calls than implementations on *nix than implementations on MacOS, etc.

  • Related