Answer to Question #180317 in C++ for Deepanshi singh

Question #180317

What are different file stream classes? Expain the syntax of opening the file in read and write mode .can while (fin) be replaced with if (fin1.eof()!=0) to process the file? Justify



1
Expert's answer
2021-04-11T06:14:19-0400

In C++, files are mainly dealt by using three classes fstream, ifstream, ofstream.

  • ofstream: This Stream class signifies the output file stream and is applied to create files for writing information to files
  • ifstream: This Stream class signifies the input file stream and is applied for reading information from files
  • fstream: This Stream class can be used for both read and write from/to files.


ptr = fopen("fileopen","mode");


where "fileopen" is the path to the file on your device, "mode" is mode in which file will be opened("r" to open in READ mode and "w" - in WRITE mode)


eof () is a member of the fstream class that returns true or false depending on whether the end of file (eof) of the file you are reading has been reached. Thus, as long as eof () returns 0, it means that the end of file has not been reached and the loop continues to run, but when eof () returns 1, the end of file is reached and the loop ends.


The while (fin) loop is introduced because fin actually returns the value of an error flag variable within a class object fin, which is set to 0 when any function, such as reading, writing, or opening, fails. Thus, the loop runs as long as the read function inside the loop is running.




Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS