Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 18 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,57 +1,33 @@
# get_next_line (short)
# Get Next Line (C)

Get Next Line is a small C implementation of get_next_line to read lines from a file descriptor. It is designed for simple file I/O tasks and learning C buffer-handling and line parsing.
Get Next Line is a compact C implementation of get_next_line for reading lines from a file descriptor. It includes example usage, a simple test harness, and minimal plain-C helper functions for buffer handling.

## Features
- Read a line from a file descriptor
- Minimal dependencies — plain C
- Example usage and simple tests included
- Read a single line from a file descriptor
- Minimal plain-C implementation (no external deps)
- Example usage and a simple test harness included

## Quick start
```sh
# build (example)
gcc -Wall -Wextra -Werror -o gnl get_next_line.c main.c
./gnl example.txt
```

## Build
make fclean all

Or compile manually:
```sh
gcc -D BUFFER_SIZE=32 -Wall -Wextra -Werror -I. get_next_line.c get_next_line_utils.c -o gnl
Build:
```

## Usage
```c
int fd = open("file.txt", O_RDONLY);
char *line;
while ((line = get_next_line(fd)) != NULL)
{
printf("%s", line);
free(line);
}
close(fd);
gcc -Wall -Wextra -Werror -o gnl get_next_line.c get_next_line_utils.c main.c
```
Returns a malloc'd string (includes `\n` if present). Caller must free.

## API
- `char *get_next_line(int fd);` — returns next line or `NULL` on EOF/error.
Run:
```
./gnl example.txt
```

## Notes
- Uses a static `leftover` buffer to keep unread data between calls.
- Avoid repeated concatenation of a growing string inside the read loop — that caus<img width="1476" height="4959" alt="Get Next Line flow chart" src="https://github.com/user-attachments/assets/555a980e-bae6-404e-bfac-0008153bc8fd" />
es O(n^2) behavior and timeouts on very large single-line files with small BUFFER_SIZE.
- For large single-line files prefer an approach that minimizes realloc/copy (chunked buffers, doubling growth, or assemble final line once).
API
- get_next_line(int fd) — returns a malloc'd string with the next line or NULL on EOF/error. See get_next_line.h for details.

## Testing
Use your usual francinette/getcheck setup:
```sh
getcheck francinette --strict
```
Files of interest
- get_next_line.c — main implementation
- get_next_line_utils.c — helper functions
- get_next_line.h — public header
- main.c — small demo/test harness

## License
This project is available under the MIT License. See LICENSE for details.

License: educational / adapt freely.

17 changes: 17 additions & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Get Next Line (C) - Lightweight C line reading implementation</title>
<meta name="description" content="Compact C implementation of get_next_line for reading lines from file descriptors. Minimal dependencies, example usage included.">
</head>
<body>
<h1>Get Next Line (C)</h1>
<p>Lightweight C implementation of get_next_line. See the README on GitHub for usage and examples.</p>
<ul>
<li><a href="https://github.com/ElyassShinwari/Get-Next-Line">View on GitHub</a></li>
<li><a href="https://github.com/ElyassShinwari/Get-Next-Line/blob/main/get_next_line.c">get_next_line.c</a></li>
</ul>
</body>
</html>