Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/whoismtrx/42_get_next_line
get_next_line a 42 curriculum project, you need to create a function that reads a line from a file descriptor.
https://github.com/whoismtrx/42_get_next_line
1337 1337cursus 1337fil 1337school 42 42born2code 42cursus 42projects 42school c clang clanguage get-next-line get-next-line-1337 get-next-line-42 gnl gnl42
Last synced: 2 days ago
JSON representation
get_next_line a 42 curriculum project, you need to create a function that reads a line from a file descriptor.
- Host: GitHub
- URL: https://github.com/whoismtrx/42_get_next_line
- Owner: whoismtrx
- Created: 2021-12-25T11:04:11.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2024-03-20T22:01:14.000Z (9 months ago)
- Last Synced: 2024-03-21T00:29:10.981Z (9 months ago)
- Topics: 1337, 1337cursus, 1337fil, 1337school, 42, 42born2code, 42cursus, 42projects, 42school, c, clang, clanguage, get-next-line, get-next-line-1337, get-next-line-42, gnl, gnl42
- Language: C
- Homepage:
- Size: 1.65 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Get_next_line
![](https://github.com/whoismtrx/42_get_next_line/blob/main/gnl.gif)
## Overview
The aim of this project is to make you code a function like getline() in C. but with some diferences. The prject is a part of the 42 curriculum, written in C language.
## Key Features
The project contains 2 parts: `Manadatory` and `Bonus`.
- `Mandatory` part is to code a function that returns a line ending with a newline, read from a file descriptor. and sould be able to memorize the reading position to read from the same file descriptor on the next call.
- `Bonus` part is to code a function that can read from multiple file descriptors without losing the reading position.## Getting Started
to compile the project you need to clone the repository first, to that run the following command:
```
git clone https://github.com/whoismtrx/42_get_next_line.git get_next_line
cd get_next_line/src
cc -Wall -Wextra -Werror -D BUFFER_SIZE=1337 get_next_line.c get_next_line_utils.c main.c
```## Usage
to use the function you need to include the header file in your project and call the get_next_line function with the file descriptor you want to read from.
```
// example.c
#include "get_next_line.h"
char *line = get_next_line(fd);
```## Implementation
The function is implemented using a static variable to store the reading position and a buffer to store the read data. the function reads the data from the file descriptor and stores it in the buffer, then it searches for a newline character in the buffer, if it finds one it returns the line ending with the newline character, if not it reads more data from the file descriptor and appends it to the buffer and repeats the process. if the function reaches the end of the file it returns the last line without a newline character. lastly, the function frees the buffer and returns the line. this process is repeated on every call to the function.
## Resources
- this is a great article about [static variables](https://www.scaler.com/topics/static-variables-in-c/).
- if you want to know more about [read](https://man7.org/linux/man-pages/man2/read.2.html) and how to use it.
- you can also read about [file descriptors](https://www.ibm.com/docs/en/aix/7.1?topic=volumes-using-file-descriptors) and how to manage them.