No video

C reading files 🔎

  Рет қаралды 49,224

Bro Code

Bro Code

2 жыл бұрын

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 жыл бұрын
#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 2 жыл бұрын
how can we do it using fscanf?!
@CorneliusCorndogJr
@CorneliusCorndogJr Жыл бұрын
your poem was truly the GOAT of all poems and to think you are a code youtuber
@QuimChaos
@QuimChaos Жыл бұрын
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...
@seancantwell12
@seancantwell12 Ай бұрын
Good catch!
@provokator-provocateur7603
@provokator-provocateur7603 2 жыл бұрын
Best explanation for reading files on youtube!
@immunewhatsapp
@immunewhatsapp Ай бұрын
In my opinion there are no better teaching channel than 'bro code'';
@ramiltaghiyev9712
@ramiltaghiyev9712 Жыл бұрын
Roses are red Violets are blue This video is awesome Scooby doo be doo
@PSIwolf39
@PSIwolf39 7 ай бұрын
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 7 ай бұрын
great, but u don't need all the #includes
@itsme-Priyaravi
@itsme-Priyaravi Ай бұрын
This video saved me from a big disaster!!!! tq so much....
@sodatechtips
@sodatechtips 3 ай бұрын
Just a quick message, you do not need != NULL in aloop, since when function fails, an execption is thrown automTically
@Mukhlisbek
@Mukhlisbek 5 ай бұрын
Very useful Thank you very much Hope you will keep making such useful contents
@salatatoe6812
@salatatoe6812 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!
@Jordan-qi2dn
@Jordan-qi2dn 2 ай бұрын
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!
@lord_toker
@lord_toker Жыл бұрын
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 10 ай бұрын
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.
@GavinDaGrey
@GavinDaGrey 9 ай бұрын
Your videos are goated, subscribing fs
@yorshex
@yorshex 2 жыл бұрын
4:00 why not to just cloes file and return 1 in this if statement?
@gerdsfargen6687
@gerdsfargen6687 Жыл бұрын
Tried this on a C compiler on an Android. Result is "Segmentation fault/core dumped". What gives?
@lord_toker
@lord_toker Жыл бұрын
Android file system =/= Windows file system is my guess
@youssofprogrammer7546
@youssofprogrammer7546 Жыл бұрын
I love your videos man
@allmyinterests5139
@allmyinterests5139 6 ай бұрын
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 Ай бұрын
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.
@NaturalKid097
@NaturalKid097 6 ай бұрын
Roses are red Violets are blue BOOTY BOOTY BOOTY BOOTY ROCKIN' EVERYWHERE!
@aymanmouhcine5749
@aymanmouhcine5749 Жыл бұрын
Very helpful playlist Thank you
@MrDritzii
@MrDritzii 2 жыл бұрын
Love you bro!
@LazaroAchon
@LazaroAchon 2 жыл бұрын
im opening one and i get a bunch of letters
@Andrii87
@Andrii87 6 ай бұрын
nicely done!
@MrCEO-jw1vm
@MrCEO-jw1vm 4 ай бұрын
learned a bunch!
@Henkita2
@Henkita2 6 ай бұрын
will you make a video on fscanf?
@mongraal2272
@mongraal2272 2 жыл бұрын
thank you very muh!
@BoredTAK5000
@BoredTAK5000 3 ай бұрын
Does anyone know if it'll work in c++?
@wxray2.0
@wxray2.0 8 ай бұрын
Thanks bro
C Tic Tac Toe game ⭕
20:08
Bro Code
Рет қаралды 155 М.
File Access Basics | C Programming Tutorial
24:05
Portfolio Courses
Рет қаралды 89 М.
Harley Quinn's plan for revenge!!!#Harley Quinn #joker
00:49
Harley Quinn with the Joker
Рет қаралды 30 МЛН
Они так быстро убрались!
01:00
Аришнев
Рет қаралды 3 МЛН
you will never ask about pointers again after watching this video
8:03
Low Level Learning
Рет қаралды 2,1 МЛН
This is Why Programming Is Hard For you
10:48
The Coding Sloth
Рет қаралды 782 М.
My 2 Year Journey of Learning C, in 9 minutes
8:42
VoxelRifts
Рет қаралды 588 М.
God-Tier Developer Roadmap
16:42
Fireship
Рет қаралды 7 МЛН
Reading/Writing structs to files (aka Serialization)
14:41
CodeVault
Рет қаралды 74 М.
why do void* pointers even exist?
8:17
Low Level Learning
Рет қаралды 351 М.
Read And Write An Array Of Structs To A Binary File | C Programming Example
18:27
My 10 “Clean” Code Principles (Start These Now)
15:12
Conner Ardman
Рет қаралды 205 М.
Reading and Writing Files in C, two ways (fopen vs. open)
7:07
Jacob Sorber
Рет қаралды 100 М.