Skip to content
Surf Wiki
Save to docs
general/c-posix-library

From Surf Wiki (app.surf) — the open knowledge base

Read (system call)

Method to access file data on a computer


Method to access file data on a computer

In modern POSIX compliant operating systems, a program that needs to access data from a file stored in a file system uses the read system call. The file is identified by a file descriptor that is normally obtained from a previous call to open. This system call reads in data in bytes, the number of which is specified by the caller, from the file and stores then into a buffer supplied by the calling process.

The read system call takes three arguments:

  1. The file descriptor of the file.
  2. The buffer where the read data is to be stored.
  3. The number of bytes to be read from the file.

POSIX usage

The read system call interface is standardized by the POSIX specification. Data from a file is read by calling the read function:

ssize_t read(int fd, void* buf, size_t count);</syntaxhighlight>

The value returned is the number of bytes read (zero indicates [[end of file]]) and the file position is advanced by this number. It is not an error if this number is smaller than the number of bytes requested; this may happen for example because fewer bytes are actually available right now (maybe because we were close to end-of-file, or because we are reading from a [[Pipe (Unix)|pipe]], or from a [[Computer terminal|terminal]]), or because the system call was interrupted by a [[Signal (computing)|signal]].

Alternatively, -1 is returned when an error occurs, in such a case [[errno]] is set appropriately and further it is left unspecified whether the file position (if any) changes.

== See also ==
* [[write (system call)]]

==References==
* [https://pubs.opengroup.org/onlinepubs/9699919799/functions/read.html POSIX read]

==External links==
* {{Wikibooks inline|C_Programming/C_Reference/stdio.h/fread}}


[[Category:C POSIX library]]
[[Category:System calls]]
Info: Wikipedia Source

This article was imported from Wikipedia and is available under the Creative Commons Attribution-ShareAlike 4.0 License. Content has been adapted to SurfDoc format. Original contributors can be found on the article history page.

Want to explore this topic further?

Ask Mako anything about Read (system call) — get instant answers, deeper analysis, and related topics.

Research with Mako

Free with your Surf account

Content sourced from Wikipedia, available under CC BY-SA 4.0.

This content may have been generated or modified by AI. CloudSurf Software LLC is not responsible for the accuracy, completeness, or reliability of AI-generated content. Always verify important information from primary sources.

Report