C reading files 🔎

preview_player
Показать описание
C read a file tutorial example explained

#C #read #file

int main()
{
char buffer[255];

if(pF == NULL)
{
printf("Unable to open file!\n");
}
else
{
while(fgets(buffer, 255, pF) != NULL)
{
printf("%s", buffer);
}
}

fclose(pF);

return 0;
}
Рекомендации по теме
Комментарии
Автор

#include <stdio.h>

int main()
{
FILE *pF = fopen("poem.txt", "r");
char buffer[255];

if(pF == NULL)
{
printf("Unable to open file!\n");
}
else
{
while(fgets(buffer, 255, pF) != NULL)
{
printf("%s", buffer);
}
}

fclose(pF);

return 0;
}

BroCodez
Автор

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

CorneliusCorndogJr
Автор

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...

QuimChaos
Автор

Best explanation for reading files on youtube!

provokator-provocateur
Автор

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

ramiltaghiyev
Автор

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

immunewhatsapp
Автор

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!

salatatoe
Автор

Your videos are goated, subscribing fs

GavinDaGrey
Автор

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

Mukhlisbek
Автор

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

sodatechtips
Автор

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

itsme-Priyaravi
Автор

Here's code I made to read a file:

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <stdlib.h>
#include <time.h>

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

PSIwolf
Автор

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!

Jordan-qidn
Автор

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?

lord_toker
Автор

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!

allmyinterests
Автор

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

gerdsfargen
Автор

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

yorshex
Автор

im opening one and i get a bunch of letters

LazaroAchon
Автор

my terminal is not workin on me, why?

Slqmerr
Автор

Mingw32 cannot open file output, help please

yusuphmwaigomole