Node:Low-level file routines, Next:, Previous:Programming with pipes, Up:Input and output

Low-level file routines

High-level file routines such as those already described are usually convenient and easy to use. However, low-level file routines such as the ones in this section have some advantages. For example, they do not treat all file input/output as text streams, as the high-level routines do; for that reason, working with binary files may be easier using low-level routines. For another thing, low-level routines do not buffer their input and output, so you will never need to remember to flush your streams with fflush or similar functions, as you sometimes must with high-level routines.

Unfortunately, because low-level routines work at a lower level of abstraction, they can be tricky, even dangerous to use -- that is to say, if used incorrectly, they may corrupt your data or cause your program to terminate unexpectedly; never fear, they will not explode your monitor or cause your computer to become sapient and attempt world domination.

As mentioned, low-level file routines do not use text streams; instead, the connection they open to your file is an integer called a file descriptor. You pass the file descriptor that designates your file to most low-level file routines, just as you pass the stream that designates your file to most high-level file routines. For example, while the low-level open function takes a filename string to open a file, the matched close function takes the file descriptor returned by open:

my_file_descriptor = open ("foo_file", O_RDONLY);
close_err = close (my_file_descriptor);