> For the complete documentation index, see [llms.txt](https://42-guide.gitbook.io/42-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://42-guide.gitbook.io/42-guide/core-curriculum/rank-01/interactive-blocks.md).

# get\_next\_line

The **"Get Next Line"** project is an essential step in learning **file handling in C**. It teaches you how to efficiently read a file **line by line**, without loading the entire file into memory at once. This is crucial when working with large files, streams, or real-time data input.

***

### **What Will You Do in This Project?**

Your task is to implement a function:

```c
char *get_next_line(int fd);
```

This function **reads and returns a single line** from a file descriptor (`fd`). If there is nothing more to read or an error occurs, it should return `NULL`.

Key requirements:\
✔️ The function must **return one line per call**, including the newline character () if it exists.\
✔️ It must handle **files and standard input (stdin)** correctly.\
✔️ Memory must be **dynamically allocated and properly freed** to prevent leaks.\
✔️ You **cannot use global variables** or certain functions like `lseek()`.\
✔️ The function should work **efficiently**, reading only the necessary amount of data.

***

### **What Will You Learn?**

#### **1. Static Variables in C**

One of the main learning goals is understanding **static variables**. These allow you to retain information across multiple function calls, which is necessary when reading a file line by line.

🔹 **Why are static variables important here?**\
Because `get_next_line()` needs to "remember" what it has already read from the file so it can return the next line correctly.

[here you can find detailed explanation for static variables](https://42-guide.gitbook.io/42-guide/all-about-c/static-variables)

***

#### **2. File Descriptors and Read Operations**

You’ll work with **file descriptors (fd)**, which are used to identify open files. You'll use the **read()** system call to extract data from a file efficiently.

🔹 **Key functions you'll use:**

* `read()` – Reads data from a file descriptor into a buffer.
* `malloc()` – Allocates memory dynamically.
* `free()` – Frees allocated memory to prevent leaks.

[About file descriptors](https://42-guide.gitbook.io/42-guide/all-about-c/file-descriptor)

***

#### **3. Efficient Memory Management**

Since `get_next_line()` deals with **dynamic memory allocation**, you must manage memory carefully to avoid leaks or excessive usage.

🔹 **Challenges you'll face:**

* Handling variable line lengths efficiently.
* Avoiding memory leaks when freeing dynamically allocated buffers.

***

#### **4. Buffer Management & Performance Optimization**

Your function should be able to handle different buffer sizes using:

```c
-D BUFFER_SIZE=n
```

This allows **changing the buffer size dynamically** when compiling your code.

🔹 **Why is this important?**

* If `BUFFER_SIZE` is too **small**, performance may suffer due to too many function calls.
* If `BUFFER_SIZE` is too **large**, you may waste memory.
* You must **balance performance and memory efficiency** in your implementation.

***

### **Bonus Part (Optional, but Highly Recommended!)**

If you complete the mandatory part, you can add **two advanced features**:\
1️⃣ **Support for Multiple File Descriptors** – Your function should be able to read from multiple files simultaneously without mixing up lines.\
2️⃣ **Using Only One Static Variable** – Improves memory efficiency and function design.

📌 **Note:** The bonus part will only be graded if your mandatory part works **perfectly**!

***

### **Final Takeaways: What Should You Know After Completing This Project?**

After finishing this project, you should have a solid understanding of:\
✅ **Reading files efficiently in C.**\
✅ **Working with file descriptors and system calls like `read()`.**\
✅ **Using static variables to retain data across function calls.**\
✅ **Managing memory effectively (malloc/free).**\
✅ **Optimizing buffer sizes for better performance.**\
✅ **Handling multiple files simultaneously (if you do the bonus).**

This project is a **fundamental building block** for many advanced topics in C, such as network programming, data processing, and system-level programming.

🚀 **Once completed, you can add `get_next_line()` to your personal C library (`libft`) for future projects!**


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://42-guide.gitbook.io/42-guide/core-curriculum/rank-01/interactive-blocks.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
