πŸ‘¨β€πŸ’»
Guide
Laura's GithubSimon's Github
  • What is this gitbook for?
  • πŸ› οΈUseful tools
    • 🏁Header files
    • 🧱C Structures
    • πŸ”—Linked Lists (todo)
    • πŸ“„Makefiles
    • πŸ”„Switch statement
    • πŸ—ƒοΈFile descriptors (FD)
  • πŸ–ŒοΈMiniLibX
    • MiniLibX Helper Function
    • MiniLibX Hook Examples
  • 0️⃣ Rank 00
    • Libft
      • πŸ“‘LIBC functions
        • ft_isalpha
        • ft_isdigit
        • ft_isalnum
        • ft_isascii
        • ft_isprint
        • ft_strlen
        • ft_memset
        • ft_bzero
        • ft_memcpy
        • ft_memmove
        • ft_strlcpy
        • ft_strlcat
        • ft_toupper
        • ft_tolower
        • ft_strchr
        • ft_strrchr
        • ft_strncmp
        • ft_memchr
        • ft_memcmp
        • ft_strnstr
        • ft_atoi
        • ft_calloc
        • ft_strdup
      • πŸ“‘Additional functions
        • ft_substr
        • ft_strjoin
        • ft_strtrim
        • ft_split
        • ft_itoa
        • ft_strmapi
        • ft_striteri
        • ft_putchar_fd
        • ft_putstr_fd
        • ft_putendl_fd
        • ft_putnbr_fd
      • πŸ“‘Bonus functions
        • ft_lstnew
        • ft_lstadd_front
        • ft_lstsize
        • ft_lstlast
        • ft_lstadd_back
        • ft_lstdelone
        • ft_lstclear
        • ft_lstiter
        • ft_lstmap
  • 1️⃣ Rank 01
    • Born2beRoot
      • πŸ“ What's a virtual machine ?
      • πŸ“ Install your virtual machine
      • πŸ“ P2P Evaluation - Questions
    • ft_printf
      • β–ͺ️Variadic functions
      • β–ͺ️Building the thing
    • get_next_line
      • β–ͺ️open() & read()
      • β–ͺ️Static variables
      • β–ͺ️Building the thing
      • β–ͺ️Commented solution
  • 2️⃣ Rank 02
    • so_long
      • β–ͺ️Understand so_long
      • β–ͺ️Core concepts
      • β–ͺ️Building the thing
    • pipex
      • β–ͺ️Understand pipex
      • β–ͺ️Functions used
      • β–ͺ️Building the thing
    • minitalk
      • β–ͺ️Understand minitalk
      • β–ͺ️Functions used
      • β–ͺ️Building the thing
    • push_swap
      • β–ͺ️Algorithms
      • β—ΎBuilding the thing
    • FdF
      • πŸ—‘οΈUnderstand FdF
      • πŸ—‘οΈGraphics programming
      • πŸ—‘οΈBuilding the thing
  • 3️⃣ RANK 03
    • Philosophers
      • β–ͺ️Understand Philosophers
      • β–ͺ️Functions used
      • β–ͺ️Building the thing
    • Minishell
      • β–ͺ️Understand Minishell
      • β–ͺ️Functions
      • β—ΎBuilding the thing
  • 4️⃣ RANK 04
    • CPP (00 - 04) (doing)
      • CPP00
      • CPP01
      • CPP02
      • CPP03
      • CPP04 (doing)
    • NetPractice
      • Theory
      • Level 1 & 2
    • MiniRT
      • Understand MiniRT
      • Building the thing
  • 5️⃣ RANK 05
    • CPP (05-09) (to-do)
      • CPP05
      • CPP06 (to-do)
      • CPP07
      • CPP08 (to-do)
      • CPP09 (to-do)
    • Inception (doing)
      • πŸ•“The basics (Docker, Images, etc...)
      • Project Files
    • webserv (to-do)
  • 6️⃣ rank 06
    • ft_transcendence (to-do)
  • πŸ›‚Exams
    • Exam Rank 02
      • Level 1
        • first_word
        • fizz_buzz
        • *ft_putstr
        • *ft_strcpy
        • *ft_strlen
        • ft_swap
        • repeat_alpha
        • rev_print
        • rot_13
        • rotone
        • search_and_replace
        • ulstr
      • Level 2
        • alpha_mirror
        • camel_to_snake
        • do_op
        • *ft_atoi
        • *ft_strcmp
        • ft_strcspn
        • ft_strspn
        • *ft_strdup
        • ft_strpbrk
        • ft_strrev
        • inter
        • last_word
        • ft_is_power_2
        • max
        • print_bits
        • reverse_bits
        • wdmatch
        • swap_bits
        • union
        • snake_to_camel
      • Level 3
        • add_prime_sum
        • epur_str
        • expand_str
        • ft_atoi_base
        • ft_list_size
        • ft_range
        • ft_rrange
        • hidenp
        • lcm
        • paramsum
        • pgcd
        • print_hex
        • rstr_capitalizer
        • str_capitalizer
        • tab_mult
      • Level 4
        • flood_fil
        • fprime
        • ft_split
        • ft_itoa
        • ft_list_foreach
        • ft_list_remove
        • rev_wstr
        • rotstring
        • sort_int_tab
        • sort_list
    • Exam Rank 03
    • Exam Rank 04
    • Exam Rank 05
      • Module 0
  • πŸ‘₯Team
Powered by GitBook
On this page
  • open ()
  • read ()

Was this helpful?

  1. 1️⃣ Rank 01
  2. get_next_line

open() & read()

Previousget_next_lineNextStatic variables

Last updated 1 year ago

Was this helpful?

To start this project you must first be able to open a text file. Once opened, we will be able to read it. This can be done using the open() and read() functions.

PS: If you don't know what it is is a file descriptor (fd), you can check this page before continuing:

open ()

For the function to work, you must first implement the following library

#include <fcntl.h>

This function will allow you to open and access a file. It is prototyped this way:

int open (const char* path, int flags [, int mode ]);

Path

It corresponds to title of the file that you would like to open/create.

It also refers to the file’s location. If you are not working in the same directory as the file, you can provide an absolute path that begins with β€œ/”

Flags

You have to tell your function what kind of access you want. This is done with flags. Here is the list with the information of each flag:

  • O_RDONLY: In read-only mode, open the file.

  • O_WRONLY: In a write-only mode, open the file

  • O_RDWR: Open the file in reading and write mode

  • O_CREAT: This flag is applied to create a file if it doesn’t exist in the specified path or directory

  • O_EXCL: Prevents the file creation if it already exists in the directory or location.

Return Value

The return value of open() is a file descriptor, a small, nonnegative integer that is an index to an entry in the process's table of open file descriptors. If there is an error somewhere, the function will return -1 as a synonym of failure.

Example

There has to be a text.txt file so you can open it.

int main()
{
    int fd;
    fd = open("text.txt", O_RDONLY);
}

For the moment it will not show you anything because the function is only used to open a file. You will have to use an additional function to make it useful. For example the read() function that we will see together now.

read ()

The function is prototyped this way:

ssize_t read(int fildes, void *buf, size_t nbyte);

This function attempts to read nbyte bytes of data from the object referenced by the descriptor fildes into the buffer pointed to by buf. The read() function starts at a position given by the pointer associated with fildes. At the end, the pointer is incremented by the number of bytes (nbyte) actually read.

It is well explained in the below video, so I will not get through it once again:

It is in french but it has english subtitles !

Now that you know how these two functions work, let's move on to the next step: . A concept that will be used a lot in other projects! See you there

β–ͺ️
πŸ—ƒοΈFile descriptors (FD)
πŸ˜„
understanding static variables