Skip to content

Program able to read a line ending with a newline character from a file descriptor

Notifications You must be signed in to change notification settings

Nkosinathi-Bonga-James-Mncube/get_next_line

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

get_next_line

What is GNL?

wtc 42

  • get_next_line()(GNL) is a program that is able to read from a textfile and display each line ending with a newline character. This this project done with only using read(), malloc(),free() and our own library libft we build previously.
  • Project done at WeThinkCode JHB campus (based on 42 france coding curriculum)


Table of contents

  1. Requirements of the project
  2. How it works
  3. Installation
  4. How to run GNL

Language used

c_programming2

  • C

Requirements of the project:

  • Create a function that returns a line read from a file descriptor
  • The following prototype must be used:
int get_next_line(const int fd, char **line);

fd = file descriptor used | line = the pointer used to store line read

  • Your function get_next_line() must return its result without \n
  • To get a full project outline, please refer to the pdf

How it works

The main goal of the project is to replicate the function fgets() to read a line from a texfile similar to the example code.

//EXAMPLE CODE
int main(int argc,char **argv)
{
        FILE * x;
        x=fopen("myfile.txt","r");
        char hold[256]; // Step 2
        while(fgets(hold,sizeof(hold),x) != NULL) // Step 1 +2
        {
                printf("%s",hold); // Step 3
        }
        fclose(x);
        return (0);
}

fgets() is function that reads a line from a stream (e.g the mytextfile.txt in this case) at "x" number of characters at a time (i.e size of string or BUFF_SIZE).On success, the function returns a line which is indentified by the \n representing end of a line.The line is stored in a variable with a allocated size(i.e hold with length of 256).When it reaches the 'End of File(EOF)' indicated by \0 it simply returns NULL.

char *fgets(char *str, int n, FILE *stream)

How to create fget()

To replicate fget() GNL is broken down into the following steps:

Step1. In main.py, the called function get_next_line() is used read each line until EOF which returns a value of -1

while ((x = get_next_line(fd, &line2)) > 0)

Step2. In get_next_line.c the function get_next_line() copies characters from file descriptor into a pointer called tempaccording to BUFF_SIZE found in get_next_line.h Here is an example for how it would work :

  1. If the BUFF_SIZE = 3
    line = "Hi\nMy name is Nathi"
    s1=[H][i][\n](first loop)
  • If no nextline character is found, the function get_next_line() keeps reading new characters and concatenates them with temp
  • If the nextline character is found the function ft_cpy()is used to create a new temp variable without the \n and returned to ft_leftover.
  • ft_leftover() is used to create new pointer,allocate memory for pointer and return pointer without nextline.
  • Lastly, the final line 'Hi' is returned in line2.

Step3. In main.py get_next_line() returns line2 to display all lines in textfile with \n added at the end

while ((x = get_next_line(fd, &line2)) > 0)
{
	ft_putstr(line2); // function from libft to display string to standard output
	ft_putchar('\n'); // function from libft to display char to standard output
	free(line2);
}

Installation

gnl_1

  1. git clone repo
  2. Enter /get_next_line folder
  3. git clone libft
  4. Enter /libft folder
  5. To compile Makefile of libft type:
make re
  1. Go back to /get_next_line folder
  2. To compile Makefile of get_next_line type:
make all

How to run get_next_line

gnl_2

  1. To execute file type:
./run.a mytext.txt

You should see

aaa
bbb
ccc

displayed to standard output from myfile.txt

Makefile commands

  • make re - recompile library when changes are made to functions
  • make clean - removes all function objects
  • make fclean - removes all function objects created with library object

About

Program able to read a line ending with a newline character from a file descriptor

Topics

Resources

Stars

Watchers

Forks