Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/archer-01/get_next_line
C function to read file one line at a time
https://github.com/archer-01/get_next_line
c memory-management static-variables
Last synced: about 15 hours ago
JSON representation
C function to read file one line at a time
- Host: GitHub
- URL: https://github.com/archer-01/get_next_line
- Owner: Archer-01
- Created: 2021-12-20T10:59:25.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2023-12-06T15:53:51.000Z (11 months ago)
- Last Synced: 2023-12-06T16:36:56.638Z (11 months ago)
- Topics: c, memory-management, static-variables
- Language: C
- Homepage:
- Size: 2.93 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Get Next Line
*Reading a line on a file descriptor is way too tedious*
## Normal Version
### Description
* A function that reads a line from a file descriptor on each call.
* Behaviour is undefined when using this function on different file descriptors### Prototype
```h
char *get_next_line(int fd);
```### Usage
```sh
git clone https://github.com/Archer-01/get_next_line.git
cd get_next_line
touch main.c
```
```c
#include
#include
#include
#include
#include "get_next_line.h"int main(void)
{
int fd;
char *line;fd = open("file.txt", O_RDONLY);
if (fd == -1)
{
fprintf(stderr, "Couldn't open file.txt");
exit(EXIT_FAILURE);
}line = get_next_line(fd);
while (line != NULL)
{
printf("%s", line);
free(line);
line = get_next_line(fd);
}
return (0);
}
```
```sh
echo "Hello from file" > file.txt
echo "Bye from file" >> file.txt
gcc -D BUFFER_SIZE=42 get_next_line.c get_next_line_utils.c main.c
./a.out
```## Bonus version
### Description
* Behaves just like the normal version but also handles multiple file descriptors at once (Up to 10240 file)
### Prototype
```h
char *get_next_line(int fd);
```### Usage
```sh
git clone https://github.com/Archer-01/get_next_line.git
cd get_next_line
touch main.c
```
```c
#include
#include
#include
#include
#include "get_next_line_bonus.h"int main(void)
{
int fd1;
int fd2;
char *line;fd1 = open("file1.txt", O_RDONLY);
fd2 = open("file2.txt", O_RDONLY);
if (fd1 == -1)
{
fprintf(stderr, "Couldn't open file1.txt");
exit(EXIT_FAILURE);
}
if (fd2 == -1)
{
fprintf(stderr, "Couldn't open file2.txt");
exit(EXIT_FAILURE);
}line = get_next_line(fd1);
printf("%s", line);
free(line);line = get_next_line(fd2);
printf("%s", line);
free(line);line = get_next_line(fd1);
printf("%s", line);
free(line);line = get_next_line(fd2);
printf("%s", line);
free(line);return (0);
}
```
```sh
echo "Hello from file 1" > file1.txt
echo "Another hello from file 1" >> file1.txt
echo "Hello from file 2" > file2.txt
echo "Another hello from file 2" >> file2.txt
gcc -D BUFFER_SIZE=42 get_next_line_bonus.c get_next_line_utils_bonus.c main.c
./a.out
```[![forthebadge](https://forthebadge.com/images/badges/made-with-c.svg)](https://forthebadge.com)