C reading files 🔎

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 ต.ค. 2021
  • C read a file tutorial example explained
    #C #read #file
    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;
    }
  • วิทยาศาสตร์และเทคโนโลยี

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

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

    #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;
    }

  • @CorneliusCorndogJr
    @CorneliusCorndogJr ปีที่แล้ว +21

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

  • @QuimChaos
    @QuimChaos ปีที่แล้ว +29

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

    Best explanation for reading files on youtube!

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

    Thank you!! Your channel helps tons!

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

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

  • @salatatoe6812
    @salatatoe6812 2 ปีที่แล้ว +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 8 หลายเดือนก่อน

    Your videos are goated, subscribing fs

  • @ramiltaghiyev9712
    @ramiltaghiyev9712 ปีที่แล้ว +12

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

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

    Very helpful playlist
    Thank you

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

    I love your videos man

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

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

  • @MrDritzii
    @MrDritzii 2 ปีที่แล้ว

    Love you bro!

  • @itsme-Priyaravi
    @itsme-Priyaravi 15 วันที่ผ่านมา

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

  • @Jordan-qi2dn
    @Jordan-qi2dn หลายเดือนก่อน

    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!

  • @MrCEO-jw1vm
    @MrCEO-jw1vm 2 หลายเดือนก่อน

    learned a bunch!

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

    nicely done!

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

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

  • @mongraal2272
    @mongraal2272 2 ปีที่แล้ว

    thank you very muh!

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

    im opening one and i get a bunch of letters

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

    will you make a video on fscanf?

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

      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.

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

    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 8 วันที่ผ่านมา +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.

  • @kayabafoxy0975
    @kayabafoxy0975 4 หลายเดือนก่อน +1

    Roses are red
    Violets are blue
    BOOTY BOOTY BOOTY BOOTY
    ROCKIN' EVERYWHERE!

  • @wxray2.0
    @wxray2.0 7 หลายเดือนก่อน

    Thanks bro

  • @gerdsfargen6687
    @gerdsfargen6687 ปีที่แล้ว +3

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

    • @lord_toker
      @lord_toker 10 หลายเดือนก่อน +3

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

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

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

  • @PSIwolf39
    @PSIwolf39 6 หลายเดือนก่อน +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);
    }

    • @jazzyBSD
      @jazzyBSD 5 หลายเดือนก่อน +1

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