Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
20.0k views
in Computer by (64.1k points)

Discuss the two methods of opening a file within a C++ program. When is one method preferred over the other?

1 Answer

+1 vote
by (69.4k points)
selected by
 
Best answer

A file can be opened in two ways :- 

(a) Using the constructor of the stream class – This method is useful when only one file is used in the stream. Constructors of the stream classes ifstream, ofstream and fstream are used to initialize the file stream object with the file name.

For example, ifstream read_file(“Names.Dat”); 

(b) Using the function open() - This method is useful when we want to use different files in the stream. If two or more files are to be processed simultaneously, separate streams must be declared for each.

For example, ifstream ifl; //input stream ifl created ifl.open(“Names.Dat”); // file Names.Dat linked with ifl

Second method is preferred over first method when there is a situation to open more than one file.

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...