What is EOF() (end of file) in FILE in C++ | Tutorial 20 | File handling in C++| Class 20|Urdu/Hindi

preview_player
Показать описание
Content Covered In This Video:
--- file handling in C++
--- Concept of eof() from file in C++
--- END OF FILE IN C++

Plz Subscribe to my channel.
If you learnt something plz Like and Share the video.

Stay tuned for more videos.
Its just a beginning.
I will upload 2 videos/Tutorials on C++ daily.

Plz Show some support.

Thank You
Рекомендации по теме
Комментарии
Автор

Hi @everyone, can someone explain me the below code related to file handling in C++. I have a doubt here in the code.

Especially inside the while loop.

while(!f_in.eof()) {

cout<<ch;
f_in>>ch;
}

As per logic, f_in>>ch should be written first, then cout statement but when I do so, its giving incorrect output as Hello!!, whereas if the above code order is followed then its giving the correct output, Hello! . That is, if f_in is written first then '!' is printed twice, "Hello!!". Please explain what's happening, and where I am getting it wrong. Thanks in advance.

while(!f_in.eof()) {


f_in>>ch;
cout<<ch;
}

My question in simple is why the above code order doesn't give the right output. That's having f_in first then cout, as that's what happens with cin & cout. At first we take i/p through cin then display it by cout.

Full Code :

#include<iostream>
#include<fstream>
using namespace std;

int main() {

ofstream f_out;
f_out.open("file.txt");
f_out<<"Hello!";
f_out.close();

ifstream f_in;
f_in.open("file.txt");

char ch;

while(!f_in.eof()) {

cout<<ch;
f_in>>ch;
}

f_in.close();

return 0;
}

AS-gfci
join shbcf.ru