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

แชร์
ฝัง
  • เผยแพร่เมื่อ 22 ต.ค. 2024

ความคิดเห็น • 49

  • @manuelmigoya2109
    @manuelmigoya2109 ปีที่แล้ว +6

    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  ปีที่แล้ว +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 ปีที่แล้ว +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  ปีที่แล้ว +1

      what do you mean by properly free?

    • @onaecO
      @onaecO  ปีที่แล้ว +2

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

    • @manuelmigoya2109
      @manuelmigoya2109 ปีที่แล้ว +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.

  • @vitalis3285
    @vitalis3285 2 หลายเดือนก่อน +2

    again a very helpful video!

  • @1337-rw6xh
    @1337-rw6xh 10 หลายเดือนก่อน +2

    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  10 หลายเดือนก่อน +1

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

  • @denis2431
    @denis2431 8 หลายเดือนก่อน +1

    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?

  • @denis2431
    @denis2431 8 หลายเดือนก่อน +1

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

  • @soufianeidrissi-bm9tc
    @soufianeidrissi-bm9tc 28 วันที่ผ่านมา

    Hi, Oceano
    first thanks, for this crazy tutorial hahahah😅
    so what should I return in this project:
    the line before we reach the null terminator '\0' and include the null terminator
    or return the line(lines) after the '\0' until the EOF

  • @MarSonyTM
    @MarSonyTM ปีที่แล้ว +1

    thank you for this video great help

    • @onaecO
      @onaecO  ปีที่แล้ว

      Ty very much!

  • @gulfstream1800
    @gulfstream1800 9 หลายเดือนก่อน +1

    good job man

  • @blackbeangamer416
    @blackbeangamer416 7 หลายเดือนก่อน

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

  • @pythonsequel2924
    @pythonsequel2924 5 หลายเดือนก่อน

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

    • @abdokar
      @abdokar หลายเดือนก่อน

      its declared in the header file

  •  8 หลายเดือนก่อน

    Thanks

    • @onaecO
      @onaecO  8 หลายเดือนก่อน

      👀👀♥️♥️ Ty u crazy!

  • @ceciledt6843
    @ceciledt6843 ปีที่แล้ว

    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  ปีที่แล้ว

      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 ปีที่แล้ว

      @@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 ! 😂👌

  • @vkatasonov
    @vkatasonov ปีที่แล้ว +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  ปีที่แล้ว

      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
      ~th-cam.com/video/gnupOrSEikQ/w-d-xo.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 ปีที่แล้ว +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=)

  • @adampassaretti9149
    @adampassaretti9149 9 หลายเดือนก่อน

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

    • @onaecO
      @onaecO  9 หลายเดือนก่อน

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

  • @__CJ.__
    @__CJ.__ ปีที่แล้ว

    Cool

  • @thebestteacher7466
    @thebestteacher7466 ปีที่แล้ว

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

    • @onaecO
      @onaecO  ปีที่แล้ว +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 ปีที่แล้ว

      @@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  ปีที่แล้ว +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!

  • @amireid8496
    @amireid8496 11 หลายเดือนก่อน +2

    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  11 หลายเดือนก่อน

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

    • @sweetquinoa8791
      @sweetquinoa8791 10 หลายเดือนก่อน

      @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  10 หลายเดือนก่อน

      come in discord discord.gg/js2mw7wBk4

  • @sodsgrooves
    @sodsgrooves ปีที่แล้ว +1

    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  ปีที่แล้ว +4

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

    • @sodsgrooves
      @sodsgrooves ปีที่แล้ว

      th-cam.com/video/-Mt2FdJjVno/w-d-xo.html great video on this topic checkout his explanation he really keeps it onsubject and leaves out the unnecessary stuff

    • @onaecO
      @onaecO  ปีที่แล้ว

      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 ปีที่แล้ว +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

  • @abdokar
    @abdokar หลายเดือนก่อน

    to be honest man, you good but the way you code is straight ahh, you make your code so complicated when it do basic stuff.

    • @onaecO
      @onaecO  หลายเดือนก่อน

      Agree! Indeed this is me trying to “fuck around to get it done” back when I was a student, If I look back to my code I 🙈🙈🙈.
      Frankly I never expected many views, this channel was me just training on explaining things