dmbcalado / Get_Next_Line

Get Next Line

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Get Next Line (grade : 112)


https://user-images.githubusercontent.com/76601369/110706242-77158d00-81ef-11eb-8085-5da6f0988553.jpg

In this project, it is intended to create a function that reads a given file and returns the next line, that means in the first iteration, gives the first line of the file, the second iteration the second line and so on. By the rules given* , and accordingly to "the Norm" of 42 school* , this has to be done in less then 5 functions of maximum 25 lines each.

The only allowed external functions were:

  • read( )
  • malloc( )
  • free( )

By studying the ssize_t read(int fildes, void *buf, size_t nbyte) we can conclude that upon successful completion, read() returns a positive integer indicating the number of bytes actually read. Otherwise, the functions shall return -1, and once was read x bytes, then the next x bytes shall be readen in the next call for the read( ) function.

We were not allowed to use global variables as it is forbidden by the "the Norm", however we were allowed to use static variables. Another important thing to mention is that we had to add the option to compile with any nbyte bigger then 1, using the call: -D BUFFER_SIZE=n.

The way i found to get to the solution of this project was by using a static char array (the buffer )that had size BUFFER_SIZE  (and if not defined then would be automatically choosen to be BUFFER_SIZE = 40) and store what was read in the file using the read( ) function as read(fd, buffer, BUFFER_SIZE).

(To be continued ...)

About

Get Next Line


Languages

Language:C 100.0%