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;
} - วิทยาศาสตร์และเทคโนโลยี
#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;
}
how can we do it using fscanf?!
your poem was truly the GOAT of all poems and to think you are a code youtuber
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...
Good catch!
Best explanation for reading files on youtube!
Thank you!! Your channel helps tons!
Very useful
Thank you very much
Hope you will keep making such useful contents
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!
Your videos are goated, subscribing fs
Roses are red
Violets are blue
This video is awesome
Scooby doo be doo
Very helpful playlist
Thank you
I love your videos man
4:00 why not to just cloes file and return 1 in this if statement?
Love you bro!
This video saved me from a big disaster!!!!
tq so much....
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!
learned a bunch!
nicely done!
Just a quick message, you do not need != NULL in aloop, since when function fails, an execption is thrown automTically
thank you very muh!
im opening one and i get a bunch of letters
will you make a video on fscanf?
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?
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.
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!
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.
Roses are red
Violets are blue
BOOTY BOOTY BOOTY BOOTY
ROCKIN' EVERYWHERE!
Thanks bro
Tried this on a C compiler on an Android. Result is "Segmentation fault/core dumped". What gives?
Android file system =/= Windows file system is my guess
Does anyone know if it'll work in c++?
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);
}
great, but u don't need all the #includes