Standard I/O Library in UNIX/Linux
The Standard I/O Library provides a high-level interface for performing
input and output operations. Instead of directly using low-level system
calls such as read() and write(), applications
can use buffered streams that simplify programming and improve
performance.
Learning Objectives
- Understand the purpose of the Standard I/O Library.
- Learn the concept of streams.
- Understand the three standard streams.
- Understand buffered I/O.
- Prepare for Standard I/O functions.
1. What is the Standard I/O Library?
The Standard I/O Library is a collection of library functions that provide a portable and convenient interface for performing input and output operations. Instead of accessing files directly through low-level system calls, programs interact with streams managed by the library.
2. Why is Standard I/O Needed?
- Simplifies file input and output.
- Provides a portable programming interface.
- Supports buffered I/O for improved efficiency.
- Offers formatted input and output functions.
- Reduces the number of direct system calls.
3. Relationship Between Standard I/O and System Calls
The Standard I/O Library is implemented on top of the low-level file I/O system calls provided by the operating system.
4. What is a Stream?
A stream is an abstraction representing a sequence of bytes flowing between a program and an input or output source. The Standard I/O Library performs all operations using streams.
5. Types of Streams
| Stream Type | Purpose |
|---|---|
| Input Stream | Transfers data into the program. |
| Output Stream | Transfers data out of the program. |
| Update Stream | Supports both reading and writing. |
6. Standard Streams
| Stream | Description | Default Device |
|---|---|---|
stdin |
Standard Input | Keyboard |
stdout |
Standard Output | Terminal |
stderr |
Standard Error | Terminal |
7. Stream Communication
8. Advantages of Standard I/O
- Portable across UNIX systems.
- Supports buffering automatically.
- Simplifies formatted input and output.
- Provides convenient file handling functions.
- Built on top of low-level file descriptors.
9. Standard I/O vs Low-Level I/O
| Standard I/O | Low-Level I/O |
|---|---|
| Uses streams | Uses file descriptors |
| Buffered by default | Generally unbuffered |
Uses FILE * |
Uses integer file descriptors |
| Higher-level interface | Lower-level interface |
10. Interview Questions
- What is the Standard I/O Library?
- Why is the Standard I/O Library used instead of low-level system calls?
- What is a stream?
- What are the three standard streams?
- What is the difference between Standard I/O and low-level I/O?
- Why does buffering improve I/O performance?
- What is an update stream?
- What is the relationship between the Standard I/O Library and
read()/write()? - Which abstraction does Standard I/O use to perform input and output?
- Why is the Standard I/O Library considered portable?
11. The FILE Structure
The Standard I/O Library represents every stream using a FILE object. Applications access streams through a pointer to this object rather than accessing its internal members directly.
12. What is FILE *?
A FILE * is a pointer to a Standard I/O stream.
It identifies an open stream that can be used by Standard I/O functions
such as fopen(), fclose(),
fread(), and fprintf().
13. FILE Object Lifecycle
14. Predefined Standard Streams
Every UNIX process begins execution with three predefined Standard I/O streams.
| Stream | Purpose | Type |
|---|---|---|
stdin |
Standard input | FILE * |
stdout |
Standard output | FILE * |
stderr |
Standard error | FILE * |
15. stdin
stdin is the predefined input stream.
By default, it receives input from the keyboard, although it may be
redirected to another source such as a file or pipe.
16. stdout
stdout is the predefined output stream.
By default, output is displayed on the terminal, but it may also be
redirected to a file or another process.
17. stderr
stderr is the predefined stream used for error messages.
Keeping error output separate from normal program output makes it easier
to diagnose problems and redirect outputs independently.
18. fileno()
The fileno() function returns the underlying file descriptor associated with an open Standard I/O stream.
This function allows interaction between the Standard I/O Library and low-level file descriptor interfaces.
19. Example: Using fileno()
On most UNIX systems, the file descriptor associated with
stdout is typically 1, although portable
programs should not rely on hard-coded descriptor values when a stream
is available.
20. Interview Questions
- What is the purpose of the
FILEstructure? - What does a
FILE *represent? - Why should applications avoid accessing the internal members of
FILE? - Name the three predefined Standard I/O streams.
- What is the default input source for
stdin? - What is the default output destination for
stdout? - Why is
stderrprovided separately fromstdout? - What is the purpose of
fileno()? - How does
fileno()connect Standard I/O with low-level I/O? - What type does
fileno()return?
21. fopen()
The fopen() function opens a file and associates it
with a Standard I/O stream.
If successful, it returns a pointer to a FILE object that
can be used for subsequent input and output operations.
22. File Opening Modes
| Mode | Description |
|---|---|
"r" |
Open an existing file for reading. |
"w" |
Create a new file or truncate an existing file for writing. |
"a" |
Open a file for writing at the end of the file (append). |
"r+" |
Open an existing file for both reading and writing. |
"w+" |
Create or truncate a file for both reading and writing. |
"a+" |
Open or create a file for reading and appending. |
23. Example: Using fopen()
24. freopen()
The freopen() function closes an existing stream and reopens it using a new file and mode. It is commonly used to redirect one stream to another file.
25. Example: Using freopen()
26. fdopen()
The fdopen() function associates an existing file
descriptor with a Standard I/O stream.
Unlike fopen(), it does not open a new file. Instead, it
creates a FILE * that uses an already-open file descriptor.
27. Example: Using fdopen()
28. fclose()
The fclose() function flushes any buffered output,
closes the associated stream, and releases the resources used by the
FILE object.
Returns 0 on success. Returns EOF on failure.
29. Stream Opening and Closing Flow
30. Interview Questions
- What is the purpose of
fopen()? - What is the difference between
"w"and"a"modes? - When should
freopen()be used? - How does
fdopen()differ fromfopen()? - Does
fdopen()create a new file descriptor? - What happens when
fclose()is called? - What does
fclose()return on failure? - Why should every successful
fopen()eventually be matched withfclose()? - Which function is commonly used to redirect
stdoutto a file? - Which function associates a Standard I/O stream with an existing file descriptor?
31. Character-Oriented I/O
Character-oriented I/O functions transfer one character at a time between a stream and a program. These functions are useful for processing text files character by character.
32. fgetc()
The fgetc() function reads the next character from the specified input stream.
Returns the next character as an unsigned char converted to
an int, or EOF if the end of file is reached
or an error occurs.
33. getc()
The getc() function performs the same logical operation
as fgetc() by reading the next character from a stream.
On many implementations, getc() may be implemented as a
macro, allowing efficient character input.
34. fgetc() vs getc()
| fgetc() | getc() |
|---|---|
| Always implemented as a function. | May be implemented as a macro. |
| Evaluates its argument once. | Macro implementations may evaluate arguments differently depending on the implementation. |
| Portable function interface. | Provides the same behavior from the programmer's perspective. |
35. Example: Using fgetc()
36. fputc()
The fputc() function writes a single character to the specified output stream.
Returns the character written as an unsigned char converted
to an int, or EOF on failure.
37. putc()
The putc() function performs the same logical operation
as fputc() by writing a character to a stream.
Like getc(), it may be implemented as a macro.
38. ungetc()
The ungetc() function pushes a character back onto an input stream. The pushed-back character is returned by the next read operation on that stream.
Only one pushed-back character is guaranteed to be supported by the C standard, although some implementations may allow more.
39. Character I/O Flow
40. Interview Questions
- What is the purpose of
fgetc()? - What is the difference between
getc()andfgetc()? - Why do
fgetc()andgetc()returnintinstead ofchar? - What value indicates end-of-file for
fgetc()? - What is the purpose of
fputc()? - How does
putc()differ fromfputc()? - What does
ungetc()do? - How many pushed-back characters does the C standard guarantee?
- When is character-oriented I/O preferred?
- Why is
EOFrepresented as anintvalue?
41. Line-Oriented I/O
Line-oriented I/O transfers an entire line of text between a stream and a program. These functions are commonly used for reading and writing text files one line at a time.
42. fgets()
The fgets() function reads at most
n - 1 characters from a stream and stores them in a buffer.
Reading stops when a newline character is read, the end of file is
reached, or the buffer becomes full.
On success, fgets() returns str.
On failure or end-of-file before any characters are read, it returns
NULL.
43. Example: Using fgets()
44. fputs()
The fputs() function writes a null-terminated string to
the specified output stream.
Unlike puts(), it does not automatically append a newline
character.
Returns a non-negative value on success.
Returns EOF on failure.
45. Example: Using fputs()
46. Binary I/O
Binary I/O transfers raw bytes between memory and a stream. Unlike text-oriented I/O, binary I/O does not interpret newline characters or text formatting.
47. fread()
The fread() function reads binary data from a stream into memory.
| Parameter | Description |
|---|---|
| ptr | Destination memory buffer. |
| size | Size of each object in bytes. |
| nmemb | Number of objects to read. |
| stream | Input stream. |
Returns the number of complete objects successfully read.
48. fwrite()
The fwrite() function writes binary data from memory to a stream.
Returns the number of complete objects successfully written.
49. Example: Binary File Copy
50. Interview Questions
- What is the purpose of
fgets()? - How does
fgets()differ fromgets()? - Why is
gets()considered unsafe? - Does
fputs()automatically append a newline character? - What is the return value of
fputs()on failure? - What is binary I/O?
- Explain the parameters of
fread(). - What does
fwrite()return? - When should binary I/O be preferred over text I/O?
- How can
fread()be used to copy a binary file?
51. Formatted Input and Output
Formatted I/O functions convert data between its internal binary representation and a human-readable text representation. These functions support conversion specifications that control how data is read from or written to streams.
52. printf() and fprintf()
The printf() function writes formatted output to
stdout.
The fprintf() function writes formatted output to any
specified stream.
53. Example: Using printf() and fprintf()
54. sprintf()
The sprintf() function writes formatted output to a character array instead of a stream. The destination buffer must be large enough to hold the resulting string.
55. snprintf()
The snprintf() function performs the same operation as
sprintf() but limits the maximum number of bytes written to
the destination buffer.
This makes it safer for fixed-size buffers.
56. Example: Using snprintf()
57. scanf(), fscanf(), and sscanf()
The formatted input functions convert text into program variables.
| Function | Input Source |
|---|---|
scanf() |
stdin |
fscanf() |
Specified stream |
sscanf() |
Character string |
58. Example: Using fscanf()
59. Formatted I/O Flow
60. Interview Questions
- What is formatted I/O?
- What is the difference between
printf()andfprintf()? - When should
sprintf()be avoided? - Why is
snprintf()considered safer? - How does
fprintf()differ fromfputs()? - What is the purpose of
scanf()? - Compare
scanf(),fscanf(), andsscanf(). - Why should the return value of formatted input functions be checked?
- What happens if the destination buffer is too small when using
sprintf()? - Which function writes formatted output to a character array with a size limit?
61. Stream Buffering
The Standard I/O Library uses buffers to reduce the number of system calls required for input and output operations. Instead of immediately performing every read or write request, data is temporarily stored in memory and transferred in larger blocks.
62. setbuf() and setvbuf()
The setbuf() and setvbuf() functions control
the buffering behavior of a stream.
| Mode | Description |
|---|---|
_IOFBF |
Fully buffered |
_IOLBF |
Line buffered |
_IONBF |
Unbuffered |
63. fflush()
The fflush() function forces any buffered output for a stream to be written to its underlying file.
Returns 0 on success. Returns EOF on failure.
64. File Positioning
The Standard I/O Library provides functions for moving and querying the current file position within a stream. These functions support random access to files.
65. fseek(), ftell(), and rewind()
| Function | Purpose |
|---|---|
fseek() |
Move the file position indicator. |
ftell() |
Return the current file position. |
rewind() |
Move to the beginning of the file. |
66. fgetpos() and fsetpos()
The fgetpos() function saves the current file position,
while fsetpos() restores a previously saved position.
These functions use the portable fpos_t type instead of a
numeric file offset.
67. Stream Error Handling
| Function | Purpose |
|---|---|
feof() |
Test the end-of-file indicator. |
ferror() |
Test the stream error indicator. |
clearerr() |
Clear both EOF and error indicators. |
perror() |
Display a message describing the current errno value. |
68. Standard I/O Workflow
70. Key Takeaways
- Standard I/O operates on
FILE *streams. - Buffering improves I/O efficiency by reducing system calls.
fopen()andfclose()manage stream lifetime.- Character, line, binary, and formatted I/O each serve different purposes.
snprintf()is generally preferred oversprintf()for fixed-size buffers.fseek(),ftell(),rewind(),fgetpos(), andfsetpos()provide file positioning.feof(),ferror(), andclearerr()help detect and recover from stream status conditions.- Always close streams with
fclose()after use.
71. Advanced Interview Questions
- Explain the relationship between Standard I/O streams and low-level file descriptors.
- When should
fdopen()be preferred overfopen()? - Compare character-oriented, line-oriented, binary, and formatted I/O.
- Explain the difference between
sprintf()andsnprintf(). - Describe the three buffering modes supported by
setvbuf(). - Differentiate
fflush()andfclose(). - Compare
fseek()/ftell()withfgetpos()/fsetpos(). - How can a program distinguish between end-of-file and an input error?
- Why should the return value of formatted input functions always be checked?
- Design a program that copies one file to another while preserving binary data.
Concept Mastery Assessment — Module 25
Verify your conceptual understanding with 12 targeted technical assessment questions. Instant feedback and explanations are provided for every choice.