get_next_line explained : develop a function that reads a file line by line

Поделиться
HTML-код
  • Опубликовано: 20 май 2024
  • 🇮🇹🍕🍝Canale Italiano - / @onaeco_ita
    . Code
    ~github.com/suspectedoceano/ge...
    Useful sources
    suspectedoceano.notion.site/g...
    #getnextline #get_next_line
    💡Algorithm from @n1kito (channel super suggested 🏆),
    ~ • coder get_next_line
    TESTER i used
    ~github.com/Tripouille/gnlTester
    LINKED LIST, top playlist
    ~ • Linked lists for absol...
    EOF, File Descriptors, static top articles
    ~ruslanspivak.com/eofnotchar/
    ~www.codequoi.com/en/handling-...
    ~www.codequoi.com/en/local-glo...
    00:00:00 Easy Intro to file descriptors
    00:09:13 dup2 function in C
    00:10:35 read system call
    00:13:52 get_next_line implementation
    00:35:55 meaning of the static keyword .
    typedef struct 🔗s_links🔗
    {
    👨🏻‍🏫 Udemy course for total beginners, pre_knowledge before C (Overflow, 2’s complement, BASH, VIM…) - www.udemy.com/course/oceano_0...
    📑 Notion community page (FREE coding sources) - suspectedoceano.notion.site/O...
    🧾 Medium (code articles) - / jalal92
    📲 Telegram group - t.me/suspectedoceanO
    💻 GitHub - github.com/suspectedoceano
    🛸 Discord - / discord
    🐦 Twitter - / suspectedoceano
    ☕️ ChannelSupport - www.buymeacoffee.com/suspecte... (if you read this, you are already supporting, TY!)
    } 🔗t_links🔗

Комментарии • 42

  • @manuelmigoya2109
    @manuelmigoya2109 10 месяцев назад +4

    Did this pass as is? I have a similar approach but I get errors with Francinette. Fsoares test read_error.txt claims there's memory leaks. Which I can see there are with valgrind. The variable returned by get_next_line() is malloc'd but never freed. Did u get similar errors with Francinette. I'm quite sure my code works, but I don't want to submit and get a fail.

    • @onaecO
      @onaecO  10 месяцев назад +3

      Hey manuel, i used only this one
      github.com/Tripouille/gnlTester
      by which all tests are ok, also no Leaks. In my cluster we only used this one, i also passed the exam with this approach, frankly i dunno i definitely shall try this tester, maybe there are some corner cases i didn t check for 🤨.

    • @manuelmigoya2109
      @manuelmigoya2109 10 месяцев назад +1

      @@onaecO I tested with valgrind and get some memory leaks. I guess that's whats flagged as error by Francinette. Though, in my own testing, I took care to properly free() the returned lines and had no issue. If you find no problem I guess I'll just push and hope for the best.

    • @onaecO
      @onaecO  10 месяцев назад +1

      what do you mean by properly free?

    • @onaecO
      @onaecO  10 месяцев назад +2

      come in the server a guy had a similar problem ;) discord.gg/2myeyu8v

    • @manuelmigoya2109
      @manuelmigoya2109 10 месяцев назад +1

      @@onaecO I ran my own test on the exact file that is returning error in Francinette. I applied valgrind and discovered that, if no free() was used, there were, in fact, memory leaks.
      So, in mine main.c file, inside the while loop, I added a free() to the returned pointer of each call of get_next_line(). This resulted in no more memory leaks detected by valgrind.
      Which might mean that it is the francinette test itself that's wrong. But I am not sure.
      This is how I wrote it:
      line = get_next_line(fd);
      while (line != NULL)
      {
      printf("%s", line);
      free(line);
      line = get_next_line(fd);
      }
      Without that free(), valgrind detects memory leaks. Right now this is the only idea I have on why it returns errors for these Francinette tests. Cause the output to console I'm seeing on my own trials is perfect.
      But I might be missing something.

  • @gulfstream1800
    @gulfstream1800 4 месяца назад

    good job man

  • @MarSonyTM
    @MarSonyTM 7 месяцев назад

    thank you for this video great help

    • @onaecO
      @onaecO  6 месяцев назад

      Ty very much!

  • @1337-rw6xh
    @1337-rw6xh 5 месяцев назад

    perfect tutorial u did it the hard way for us as beginners using linked list but it's okay the important thing is that i got the full idea of what i need to do, much love 🙏

    • @onaecO
      @onaecO  5 месяцев назад +1

      Ty! Yeah major pain tutorial lol I just wanted to train myself. There are much easier ways

  • @__CJ.__
    @__CJ.__ 11 месяцев назад

    Cool

  •  3 месяца назад

    Thanks

    • @onaecO
      @onaecO  3 месяца назад

      👀👀♥️♥️ Ty u crazy!

  • @denis2431
    @denis2431 2 месяца назад

    thanks oceano good explanation, the only thing i cannot understand is why in the while condition of the void create_list function. the subfunction found_newline finds 1 means there is '
    ', so by logic it should stop the condition and not copy the last list which contains the '
    ', but in your explanation it copy everything, where i misunderstood?

  • @vkatasonov
    @vkatasonov 9 месяцев назад +1

    Thx for the video=) Despite on I've made a project, there was a bunch of interesting information.
    One question: which IDE do you use. And could you pls share some basic settings of your .vimrc file? I see that you have autocomplete option in it. And, honestly, I don't know how to switch it on.
    Many thx in advance.

    • @onaecO
      @onaecO  9 месяцев назад

      Bro, Thx for the comment!
      As you know these files are super tedious to decipher..i should properly make a video about.
      Anyway most ideas you can get from here
      ~ruclips.net/video/gnupOrSEikQ/видео.html
      this is the actual file, feed this to GPT + video and i think wont be super hard
      vimrc
      syntax on
      set number
      set tabstop=4
      set shiftwidth=0
      set backspace=indent,eol,start
      filetype plugin on
      call plug#begin('~/.vim/plugged')
      Plug 'davidhalter/jedi-vim'
      Plug 'sheerun/vim-polyglot'
      Plug 'nvie/vim-flake8'
      Plug 'tpope/vim-commentary'
      Plug 'neoclide/coc.nvim', {'branch': 'release'}
      Plug '42Paris/42header'
      Plug 'octol/vim-cpp-enhanced-highlight'
      "Plug 'ycm-core/YouCompleteMe', {'do': 'python3 install.py --clangd-completer'}
      "Plug 'valloric/youcompleteme'
      Plug 'vim-syntastic/syntastic'
      Plug 'preservim/nerdtree'
      Plug 'pboettch/vim-cmake'
      Plug 'ryanoasis/vim-devicons'
      Plug 'tiagofumo/vim-devicons-colorschemes'
      Plug 'vim-airline/vim-airline'
      Plug 'vim-airline/vim-airline-themes'
      " Python development plugins
      call plug#end()
      let g:jedi#force_py_version = 3
      let g:jedi#auto_complete = 1
      let g:jedi#show_docstring = 1
      let g:jedi#show_call_signatures = 1
      let g:jedi#auto_completion_start_timeout = 0
      let g:jedi#auto_complete_delay = 0
      let g:jedi#auto_complete_trigger_length = 1
      " Set the Airline theme
      let g:airline_theme='onedark'
      "Automatic nerdtree
      map :NERDTreeToggle
      "
      "colors devicons
      "colorscheme murphy
      "DEBUG HIGHLIGHT
      highlight DEBUG ctermfg=black ctermbg=yellow guifg=black guibg=yellow
      "vim no split
      nnoremap :edit
      "COC stuff
      " Use the clangd language server for C and C++
      let g:coc_global_extensions = ['coc-clangd']
      " Enable completion for C and C++
      "autocmd FileType c,cpp inoremap
      " \ pumvisible() ? "\" :
      " \ check_back_space() ? "\" :
      " \ coc#refresh()
      "function! s:check_back_space()
      " let col = col('.') - 1
      " return !col || getline('.')[col - 1] =~ '\s'
      "endfunction
      " Enable diagnostics (errors and warnings)
      let g:coc_global_extensions = ['coc-clangd', 'coc-diagnostic']
      " Show diagnostics in the sign column
      autocmd CursorHold * silent call CocActionAsync('diagnosticRefresh')
      autocmd CursorHoldI * silent call CocActionAsync('diagnosticRefresh')
      "Enter to select suggestion
      inoremap pumvisible() ? "\" : "\"
      " Use a lighter sign column
      highlight SignColumn ctermbg=NONE
      highlight SignColumn guibg=NONE
      " Configure coc-clangd
      let g:coc_clangd_binary = 'clangd' " Path to clangd binary
      let g:coc_clangd_args = ['-header-insertion=never'] " Arguments for clangd
      " Define the color for C warnings
      highlight cWarn ctermfg=red guifg=red
      " Set the syntax highlighting for C warnings
      autocmd Syntax c syn keyword cWarn contained warning
      " Set the color of diagnostic error messages
      highlight CocErrorSign ctermfg=white ctermbg=red guifg=#ffffff guibg=#ff0000

    • @vkatasonov
      @vkatasonov 9 месяцев назад +1

      @@onaecO By the way... I don't know if you like it, but I was a bit disgusted about warning style icon in Coc (that ridiculous triangle with exlamation mark inside). So I've changed the rule a little bit=)))
      " Set warning color and sign
      highlight CocWarningSign guifg=red ctermfg=red
      let g:coc_user_config = {
      \ 'diagnostic': {
      \ 'warningSign': '!!'
      \ }
      \}
      Now it looks much prettier=)

  • @ceciledt6843
    @ceciledt6843 8 месяцев назад

    Hey ! Thanks a lot for your vidéo, it helps a lot to understand more !
    For dealloc function, why did you free buf only if clean node is NULL ? And not in any case ? And why do you bring it to dealloc and don't free it directly in polish ?

    • @onaecO
      @onaecO  8 месяцев назад

      Hello Cecile! I try to remember what i wrote 🤣
      Here i say: “if str_buf contains at least 1 char, make this node the new head for next call to gnl”
      else
      “clean the buffer and the node”
      if (clean_node->str_buf[0])
      *list = clean_node;
      else
      {
      free(buf);
      free(clean_node);
      }
      So if the buffer is empty, i don’t need it. Of course this is bad design code (i already feel cringe for old code 🤣), i could have used only clean_node struct that already contains the buffer, if i correctly recall my code.
      So i don’t free in every case simply because if there is a string inside buf, i’ll need the at the next gnl call.
      Polish is a special function to clean the linked list last node up to
      , so i decided to keep the clean up deallocs in another function.
      I hope i responded u!

    • @ceciledt6843
      @ceciledt6843 8 месяцев назад

      @@onaecO Thank you so much for you answer !
      I think now it's more clear for me :
      - buf and clean_node->str are pointing the same adress so if you clean buf you clean somehow clean_node->str too ?
      And don't be ashamed of your code : for beginners like me it's dynamite seriously ! 😂👌

  • @denis2431
    @denis2431 2 месяца назад

    19:37 I mean why you read first and after check while condition?

  • @pythonsequel2924
    @pythonsequel2924 11 дней назад

    Can you explain where the str_buf comes from the the append function? It’s not declared anywhere.

  • @blackbeangamer416
    @blackbeangamer416 2 месяца назад

    is there a reason why the "Append" function is not in the header file ?

  • @thebestteacher7466
    @thebestteacher7466 10 месяцев назад

    is there reasaon why we need to define BUFFER_SIZE 10 instead of just writing 10? or is it just convenient?

    • @onaecO
      @onaecO  10 месяцев назад +1

      The thing is that BUFFER_SIZE is a macro defined at compile time, it can be 1 , 42, 1 million, whatever. If you check my code i have in the .h file
      github.com/suspectedoceano/get_next_line/tree/main
      you find this
      # ifndef BUFFER_SIZE
      # define BUFFER_SIZE 10
      # endif
      Just in case the value is not provided during compilation.
      Hardcoding 10 misses the point of the exercise, i.e. handling whatever buffer size. I hope i answered u ;)

    • @thebestteacher7466
      @thebestteacher7466 10 месяцев назад

      @@onaecO ohhhhh so the reason why we marcro defined is because we can change all of it instantly without having to keep chaning all the variable one by one.

    • @onaecO
      @onaecO  10 месяцев назад +1

      @@thebestteacher7466 Bingo! that’s exactly why they are useful.
      This “refactor the code” instantly.
      The more variables & constants , the better the code. Hard type values is bad, search for “magic numbers code”.
      Good work!

  • @adampassaretti9149
    @adampassaretti9149 4 месяца назад

    There is a reason you chosed to implement the project with linked list? It should be less efficient from what i know.

    • @onaecO
      @onaecO  4 месяца назад

      Hey, not really. Just to train myself on linked lists. This is arguably a bad implementation indeed 😁

  • @amireid8496
    @amireid8496 5 месяцев назад +1

    I am a huge fan, and I always follow you.. But I am really sorry to tell you that this video made the task even harder for a beginner. the fact you are using linked lists approach while beginners just got introduced to the linked lists in the bonus part of libft and they havent even properly used it and got handy with it, would definitely make me not suggest this video for a beginner. Sorry, and keep it up. cheers!

    • @onaecO
      @onaecO  5 месяцев назад

      Yeah you right! Indeed I just wanted to train myself here, there are much simpler implementations. Ty for feedback ✌️

    • @sweetquinoa8791
      @sweetquinoa8791 5 месяцев назад

      @1337-rw6xh @amireid8496 How would you approach this project, then? I'm about to start it, and I'm feeling a bit lost. Thank you!

    • @onaecO
      @onaecO  5 месяцев назад

      come in discord discord.gg/js2mw7wBk4

  • @sodsgrooves
    @sodsgrooves 9 месяцев назад

    Never seen such a complicated explaination on this topic try to switch not to much and keep your focus on the subject. I have seen this video and compared to other things on the internet this seem like higher math.

    • @onaecO
      @onaecO  9 месяцев назад +4

      Thx for the comment
      I tried my best, i know it's not easy

    • @sodsgrooves
      @sodsgrooves 9 месяцев назад

      ruclips.net/video/-Mt2FdJjVno/видео.html great video on this topic checkout his explanation he really keeps it onsubject and leaves out the unnecessary stuff

    • @onaecO
      @onaecO  9 месяцев назад

      if you read the description , i had already mentioned the video. I just wanted to add smtg to that explanation , failing as i see
      Thx again for feedback

    • @stanislavlia
      @stanislavlia 8 месяцев назад +1

      Actually, I found your explanation and code quite understandable and useful. I decided to stick to your approach to this problem. Thank you sirrr :)@@onaecO