Renaming all folders
This commit is contained in:
26
03_BufferedIO/README.md
Normal file
26
03_BufferedIO/README.md
Normal file
@@ -0,0 +1,26 @@
|
||||
# Chapter 3 – Buffered I/O
|
||||
|
||||
## Difference Between Byte-Level and Buffered I/O
|
||||
- **System calls** like `read()` and `write()` operate at a low level: each call may result in direct disk access.
|
||||
- **C libraries** use buffered I/O (`fread()`, `fwrite()`, `fprintf()`), accumulating data in memory to reduce system calls.
|
||||
|
||||
## Buffered I/O API
|
||||
- Uses `FILE *` type from `<stdio.h>`.
|
||||
- Functions:
|
||||
- `fopen()`, `fclose()`: open/close file.
|
||||
- `fread()`, `fwrite()`: binary I/O.
|
||||
- `fprintf()`, `fscanf()`, `fgets()`, `fputs()`: formatted or text I/O.
|
||||
|
||||
## Types of Buffering
|
||||
- **Fully buffered**: flush when buffer is full.
|
||||
- **Line buffered**: flush at end of line (e.g., terminals).
|
||||
- **Unbuffered**: no accumulation (e.g., stderr).
|
||||
|
||||
## Buffer Management
|
||||
- `setvbuf()` or `setbuf()` allows custom buffering.
|
||||
- Useful for optimizing performance or interactive environments.
|
||||
|
||||
## When to Use Buffered I/O
|
||||
- When performance matters and immediate file system synchronization isn't required.
|
||||
- For formatted I/O, the C library offers more simplicity and readability.
|
||||
|
||||
48
03_BufferedIO/binarydata.c
Normal file
48
03_BufferedIO/binarydata.c
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: MIT
|
||||
* Copyright (c) 2025 Fabio Scotto di Santolo
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
FILE *in, *out;
|
||||
struct pirate {
|
||||
char name[100]; /* real name */
|
||||
unsigned long booty; /* in pounds sterling */
|
||||
unsigned int beard_len; /* in inches */
|
||||
} p, blackbeard = {"Edward Teach", 950, 48};
|
||||
|
||||
out = fopen("data", "w");
|
||||
if (!out) {
|
||||
perror("fopen");
|
||||
return 1;
|
||||
}
|
||||
if (!fwrite(&blackbeard, sizeof(struct pirate), 1, out)) {
|
||||
perror("fwrite");
|
||||
return 1;
|
||||
}
|
||||
if (fclose(out)) {
|
||||
perror("fclose");
|
||||
return 1;
|
||||
}
|
||||
|
||||
in = fopen("data", "r");
|
||||
if (!in) {
|
||||
perror("fopen");
|
||||
return 1;
|
||||
}
|
||||
if (!fread(&p, sizeof(struct pirate), 1, in)) {
|
||||
perror("fread");
|
||||
return 1;
|
||||
}
|
||||
if (fclose(in)) {
|
||||
perror("fclose");
|
||||
return 1;
|
||||
}
|
||||
printf("name=\"%s\" booty=%lu beard_len=%u\n", p.name, p.booty,
|
||||
p.beard_len);
|
||||
return 0;
|
||||
}
|
||||
14
03_BufferedIO/input.c
Normal file
14
03_BufferedIO/input.c
Normal file
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: MIT
|
||||
* Copyright (c) 2025 Fabio Scotto di Santolo
|
||||
*/
|
||||
|
||||
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
printf("Limit buffer size is %u bytes.\n", LINE_MAX);
|
||||
printf("Buffer size is %u bytes.\n", BUFSIZ);
|
||||
}
|
||||
Reference in New Issue
Block a user