<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Errfig Aymen</title>
    <description>The latest articles on DEV Community by Errfig Aymen (@aerrfig).</description>
    <link>https://dev.to/aerrfig</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1645312%2Fbcf9dccc-b956-44de-9e2f-3e8066af0357.jpeg</url>
      <title>DEV Community: Errfig Aymen</title>
      <link>https://dev.to/aerrfig</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aerrfig"/>
    <language>en</language>
    <item>
      <title>GET NEXT LINE A 42 Project TO Learn How To Deal with File Descriptors and I/O of System</title>
      <dc:creator>Errfig Aymen</dc:creator>
      <pubDate>Sun, 06 Oct 2024 13:11:50 +0000</pubDate>
      <link>https://dev.to/aerrfig/get-next-line-a-42-project-to-learn-how-to-deal-with-file-descriptors-and-io-of-system-3652</link>
      <guid>https://dev.to/aerrfig/get-next-line-a-42-project-to-learn-how-to-deal-with-file-descriptors-and-io-of-system-3652</guid>
      <description>&lt;p&gt;In the realm of C programming, managing input, output, and memory effectively is fundamental. To help you grasp these critical concepts, &lt;code&gt;get_next_line&lt;/code&gt; is a project where you'll write a function that reads a file line by line using a file descriptor. Each invocation of the function reads the next line from the file, allowing you to process the entire file content one line at a time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding File Descriptors and I/O in a System
&lt;/h2&gt;

&lt;h2&gt;
  
  
  What is a File Descriptor?
&lt;/h2&gt;

&lt;p&gt;A &lt;code&gt;file descriptor&lt;/code&gt; is a non-negative integer that uniquely identifies an open file in a system. When a program opens a file, the operating system returns a file descriptor that can be used to refer to that file in subsequent operations, such as reading, writing, or closing the file. File descriptors are an abstraction used by the operating system to manage various I/O resources, including files, sockets, and pipes.&lt;/p&gt;

&lt;p&gt;0, 1, and 2 (standard input, standard output, and standard error) in Process A are independent and separate from the file descriptors in Process B. This isolation ensures that file operations in one process do not interfere with those in another.&lt;/p&gt;

&lt;h3&gt;
  
  
  file descriptor table
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw9mlhhnr5zh24901ae5y.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw9mlhhnr5zh24901ae5y.jpeg" alt="file descriptor table"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Each &lt;code&gt;file descriptor&lt;/code&gt; is associated with a file descriptor table entry that contains essential information about the file. This includes the file path, access permissions, and the current offset, which tracks the position within the file for &lt;code&gt;read/write operations&lt;/code&gt;. This structure allows the operating system to manage multiple open files efficiently and ensure correct access and data manipulation.&lt;/p&gt;

&lt;p&gt;Note that file descriptors 0, 1, and 2 are reserved by the operating system for standard streams. File descriptor 0 is used for standard input (stdin), which typically represents input from the keyboard. File descriptor 1 is used for standard output (stdout), which represents output to the screen or terminal. File descriptor 2 is used for standard error (stderr), which also represents output to the screen or terminal but is specifically intended for error messages. These reserved file descriptors ensure that basic input and output operations can be consistently managed across different programs and environments. Any file descriptor returned by the open function will be 3 or higher, ensuring it does not conflict with these standard streams.&lt;/p&gt;

&lt;h3&gt;
  
  
  how to open file
&lt;/h3&gt;

&lt;h5&gt;
  
  
  example
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'#include &amp;lt;fcntl.h&amp;gt;'
'#include &amp;lt;unistd.h&amp;gt;'

int fd = open("example.txt", O_RDONLY);
if (fd == -1) {
    perror("Error opening file");
    return 1;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  code breakdown
&lt;/h5&gt;

&lt;p&gt;A file descriptor, represented as an integer, is obtained using the &lt;code&gt;open&lt;/code&gt; function, which takes two parameters: the file name (or path) and flags that determine the file's access permissions. For example, to read a file's content, we use the O_RDONLY flag (read-only). To read and write, we use the O_RDWR flag. While there are many flags available, we will use only O_RDONLY for this project. The &lt;code&gt;open&lt;/code&gt; function returns a non-negative integer, which is the &lt;code&gt;file descriptor&lt;/code&gt; if the operation is successful; otherwise, it returns -1 to indicate an error (you don't have permission to access example.txt). Note that the open function is in the unistd.h library, and the permission flags are defined in fcntl.h.&lt;/p&gt;

&lt;h3&gt;
  
  
  reading from a file descriptor
&lt;/h3&gt;

&lt;h5&gt;
  
  
  example
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'#include &amp;lt;fcntl.h&amp;gt;'
'#include &amp;lt;unistd.h&amp;gt;'
'#include &amp;lt;stdio.h&amp;gt;'
'#define BUFFER_SIZE 4'

int fd = open("example.txt", O_RDONLY);
if (fd == -1) {
    perror("Error opening file");
    return 1;
}
char buffer[BUFFER_SIZE];
read(fd, buffer, sizeof(buffer)-1);
printf("1st call : %s\n", buffer);
// prints the first 3 bytes
read(fd, buffer, sizeof(buffer)-1);
printf("2nd call : %s\n", buffer);
read(fd, buffer, sizeof(buffer)-1);
printf("3rd call : %s\n", buffer);
read(fd, buffer, sizeof(buffer)-1);
printf("4th call : %s\n", buffer);
read(fd, buffer, sizeof(buffer)-1);
printf("5th call : %s\n", buffer);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  breakdown
&lt;/h5&gt;

&lt;p&gt;code result&lt;br&gt;
&lt;code&gt;&lt;br&gt;
1st call : HEL&lt;br&gt;
2nd call : LO &lt;br&gt;
3rd call : WOR&lt;br&gt;
4th call : LD&lt;br&gt;
5th call : (null)&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
The &lt;code&gt;read&lt;/code&gt; function, provided by the unistd.h library, is used to read data from a file descriptor. It takes three parameters: the file descriptor, a buffer to store the read data, and the number of bytes to read from the file, read function returns the number of bytes read from the file.&lt;/p&gt;

&lt;p&gt;In the file descriptor table, there's an attribute called &lt;code&gt;offset&lt;/code&gt;. The offset keeps track of the current position within the file. Every time the read function is called, it reads data starting from the current offset and then advances the offset by the number of bytes read. This ensures that subsequent reads continue from where the last read left off.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl9jm5v09m3xkbsu724wm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl9jm5v09m3xkbsu724wm.png" alt="how offset works"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In our example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The first call to read reads the first 3 bytes from the file and stores them in the buffer, starting at the beginning of the file (offset 0). The offset is then updated to 3.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The second call to read reads the next 3 bytes starting from the updated offset (3), then updates the offset to 6.&lt;br&gt;
etc ...&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;5th call to read buffer will be null and read returns 0 indicating end of file.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This process continues until all the data has been read from the file or an error occurs. The buffer is null-terminated after each read to ensure it can be printed as a string.&lt;/p&gt;

&lt;h2&gt;
  
  
  THE PROBLEM
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;char *get_next_line(int fd)&lt;/code&gt; takes as parameter a file descriptor of a file and returns one line for each call. If it reaches the end of the file, it returns NULL.&lt;/p&gt;

&lt;h4&gt;
  
  
  Parameters
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;fd&lt;/strong&gt;: File descriptor of the file to read from.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;BUFFER_SIZE&lt;/strong&gt;: The size of the buffer used to read chunks from the file.
your program should have no leaks.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Solution :
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://github.com/Its-JoeTheKing/get_next_line" rel="noopener noreferrer"&gt;https://github.com/Its-JoeTheKing/get_next_line&lt;/a&gt;&lt;/p&gt;

</description>
      <category>c</category>
      <category>memory</category>
      <category>algorithms</category>
      <category>systems</category>
    </item>
  </channel>
</rss>
