C reading files 🔎

Поделиться
HTML-код
  • Опубликовано: 18 янв 2025

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

  • @BroCodez
    @BroCodez  3 года назад +35

    #include
    int main()
    {
    FILE *pF = fopen("poem.txt", "r");
    char buffer[255];
    if(pF == NULL)
    {
    printf("Unable to open file!
    ");
    }
    else
    {
    while(fgets(buffer, 255, pF) != NULL)
    {
    printf("%s", buffer);
    }
    }
    fclose(pF);
    return 0;
    }

    • @mounikareddy6395
      @mounikareddy6395 3 года назад

      how can we do it using fscanf?!

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

      Hello bro thanks for your amazing teaching,but I have problem of reading file using mingw32

  • @sigmaohiophonk
    @sigmaohiophonk Год назад +35

    your poem was truly the GOAT of all poems and to think you are a code youtuber

  • @Elcinhmdzad
    @Elcinhmdzad 2 месяца назад +21

    Roses are red,
    Violets are blue,
    You grind every day,
    Bro, I respect you.
    Coding all night,
    With coffee in hand,
    Debugging those errors,
    I totally understand.
    Stay on the hustle,
    You’ve got what it takes,
    Bro, keep on pushing,
    Success never breaks. 🌟

    • @LilNuggette
      @LilNuggette 2 месяца назад +1

      bro... this is beautiful

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

      @LilNuggette thank you

  • @QuimChaos
    @QuimChaos 2 года назад +45

    if anyone is getting an error/segmentation fault when testing with a non existent file, move the fclose() inside the else. fclose can't close a file that is not open so it will segment fault...

  • @provokator-provocateur7603
    @provokator-provocateur7603 3 года назад +6

    Best explanation for reading files on youtube!

  • @immunewhatsapp
    @immunewhatsapp 6 месяцев назад +2

    In my opinion there are no better teaching channel than 'bro code'';

  • @ramiltaghiyev9712
    @ramiltaghiyev9712 2 года назад +16

    Roses are red
    Violets are blue
    This video is awesome
    Scooby doo be doo

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

    Very useful
    Thank you very much
    Hope you will keep making such useful contents

  • @salatatoe6812
    @salatatoe6812 3 года назад +2

    Hey man awesome video! Would it be possible if you made a video on registering a word and line number where the word first appears in a dictionary? And then print the words and line numbers.
    Have an awesome day!

  • @GavinDaGrey
    @GavinDaGrey Год назад

    Your videos are goated, subscribing fs

  • @itsme-Priyaravi
    @itsme-Priyaravi 7 месяцев назад

    This video saved me from a big disaster!!!!
    tq so much....

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

    Just a quick message, you do not need != NULL in aloop, since when function fails, an execption is thrown automTically

  • @yorshex
    @yorshex 2 года назад +2

    4:00 why not to just cloes file and return 1 in this if statement?

  • @lord_toker
    @lord_toker Год назад +2

    What if I want to read from a specific line? Using your file as an example, what would I need to do to make it read just BOOTY BOOTY BOOTY BOOTY in stdout?

    • @connerd5295
      @connerd5295 Год назад +2

      if you know the number of which line BOOTY BOOTY BOOTY BOOTY is on, you can make a counter which counts each loop and put the print statement inside an if(counter == the correct line number). then the program wont print any of the other lines in the file.

  • @Jordan-qi2dn
    @Jordan-qi2dn 7 месяцев назад +1

    I noticed some weird behaviour with this one, what could the case be?
    Initially I made a text document with 3 lines, the while loop skipped the first line and printed the remaining 2 no problem.
    I edited the file and put the numbers 1-6 on respective lines, and it only printed the even ones. I don't understand why this is happening!

  • @youssofprogrammer7546
    @youssofprogrammer7546 2 года назад

    I love your videos man

  • @Aminerami5749
    @Aminerami5749 Год назад

    Very helpful playlist
    Thank you

  • @LazaroAchon
    @LazaroAchon 2 года назад +2

    im opening one and i get a bunch of letters

  • @allmyinterests5139
    @allmyinterests5139 11 месяцев назад

    Very nice explanation thank you so much! I only got one question: your char array consists of 255 chars and you allow fgets() to read 255 characters. But a string always needs \0 at its end so I am used to either declare the string like: char buffer[255 + 1] or read one element less like: fgets(buffer, 254, pF). Isn't that necessary when reading files or was it a mistake in the code? Cheers!

    • @seancantwell12
      @seancantwell12 6 месяцев назад +1

      From ChatGPT:
      You're correct. A null-terminator (`\0`) is necessary to mark the end of a C string. If you have an array of 255 characters, reading exactly 255 characters without accounting for the null-terminator can lead to undefined behavior or buffer overflow issues.

  • @MrDritzii
    @MrDritzii 3 года назад

    Love you bro!

  • @Henkita2
    @Henkita2 11 месяцев назад

    will you make a video on fscanf?

  • @gerdsfargen6687
    @gerdsfargen6687 Год назад +3

    Tried this on a C compiler on an Android. Result is "Segmentation fault/core dumped". What gives?

    • @lord_toker
      @lord_toker Год назад +3

      Android file system =/= Windows file system is my guess

  • @Andrii87
    @Andrii87 11 месяцев назад

    nicely done!

  • @mongraal2272
    @mongraal2272 2 года назад

    thank you very muh!

  • @MrCEO-jw1vm
    @MrCEO-jw1vm 9 месяцев назад

    learned a bunch!

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

    my terminal is not workin on me , why?

  • @PSIwolf39
    @PSIwolf39 Год назад +1

    Here's code I made to read a file:
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    int main(){
    FILE *pFile = fopen("C:\\Users\\squee\\Desktop\\Hello.txt","r");
    if(pFile == NULL){
    printf("Hello.txt does not exist :(");
    }
    else{
    char buffer[256];
    while(fgets(buffer,256,pFile) != NULL){
    printf("%s",buffer);
    }
    }
    fclose(pFile);
    }

    • @jazzyBit
      @jazzyBit Год назад +2

      great, but u don't need all the #includes

  • @wxray2.0
    @wxray2.0 Год назад

    Thanks bro

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

    Mingw32 cannot open file output ,help please

  • @liloppai8
    @liloppai8 Месяц назад

    Hi i have followed your video step by step. Unfortunately i am still tagging for "Error: could not open file .txt for reading". I do not understand as to why i am, if anyone has any answers PLS HELP.

    • @User67470
      @User67470 Месяц назад

      Are compile on android, if yes then that's the problem, it requires a laptop compiler e.g VS code

  • @ChrisKoledrianos
    @ChrisKoledrianos 3 месяца назад +1

    0:10 can you dm me the codes inside the first folder I need them for a prank

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

    Does anyone know if it'll work in c++?

    • @thefallen3721
      @thefallen3721 2 дня назад

      Any code in C will work in C++. But not all codes in c++ will work in c.