get_next_line

Table of Contents

About

FULL SUBJECT HERE

Get_next_line is a function that returns a line read from a file descriptor.

External functions used: read, malloc, free

💾 Prototype

char *get_next_line(int fd);

⚙️ Parameters

  • fd: File descriptor to read from

Return value

  • Read line: correct behavior
  • (NULL): nothing else to read or an error occurred

Concepts

To get the most of out of this projects, students should deepen their understanding of two important concepts: static variables and file descriptors.

Static Variables

Static variables have a property of preserving their value even after they are out of scope. In some ways, they work as global variables. Hence, static variables preserve the previous value assigned in the previous scope and are not reinitalized in the new scope.

static int answer = 42;

If a program runs a block of code, exists it and goes on to another part of the program, and then finally calls that same block of code, the static variable will have preserved the previous value of that variable.

File Descriptors

A file descriptor is a number that uniquely identifies an open file in a computer's operating system. It describes the data resource (file, website, database) and how to access it.

When a program asks to open a file, or another data resource, like a network socket, the kernel:

1. grants access
2. creates an entry in the global file table
3. provides the software with the location (address) of that entry.

When a set of instructions make a successful request to open a file, the kernel returns a file descriptor that points to an entry in the kernel's global file table. The file table entry contains information such as the inode of the file (a.k.a. index node - a description of a file-system objects such as a file or a directory), byte offset, and the access restrictions for that data stream (read-only, write-only, etc.).



42 Adelaide logo