26 Nov 2019

  • November 26, 2019
  • Amitraj
C++ File and File Modes

C++ language allows us to perform important Disk input/output operations such as -

-> Creating a new file on the disk.
-> Reading the file stored on the disk.
-> Writing data to the file stored on the disk.
-> Appending new data to the end of the file stored on the disk.
-> Modifying the content of the file stored on the disk.


To perform any file operations, C++ provides us a few file stream classes, such as -

-> ifstream, to perform the file input operations.
-> ofstream, to perform the file output operations.

-> fstream, to perform any file input and output operations.


-> In C++, for every file operation, exists a specific file mode. These file modes allow us to create, read, write, append or modify a file. The file modes are defined in the class ios. Let's see all these different modes in which we could open a file on disk.



File ModesDescription
ios::in
Searches for the file and opens it in the read mode only(if the file is found).
ios::out
Searches for the file and opens it in the write mode. If the file is found, its content is overwritten. If the file is not found, a new file is created. Allows you to write to the file.
ios::app
Searches for the file and opens it in the append mode i.e. this mode allows you to append new data to the end of a file. If the file is not found, a new file is created.
"ios::binary"
Searches for the file and opens the file(if the file is found) in a binary mode to perform binary input/output file operations.
ios::ate
Searches for the file, opens it and positions the pointer at the end of the file. This mode when used with ios::binary, ios::in and ios::out modes, allows you to modify the content of a file.
"ios::trunc"
Searches for the file and opens it to truncate or deletes all of its content(if the file is found.
"ios::nocreate"
Searches for the file and if the file is not found, a new file will not be created.



-> This concept of file modes has introduced you to some important disk input/output operations that we can perform on a file and modes in which we can open a file on disk. 

Related Posts:

  • Friend Class and Friend Function in C++ Friend - Class:-  A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful to allow a particular class to access private members of other class.… Read More
  • Different paradiagrams for problem solving Programming paradigm: Programming paradigm means that the fundamental of computer programming. Programming style or methods are changed from language to language. There are three programming paradigm – 1.) Imperative / Proc… Read More
  • Operators in C and C++ Operators in C and C++ 1.) Arithmetic Operator:-              The type of operators are use to perform general mathematical functions such as Add, Sub, Multiply or Divide. -> The… Read More
  • Difference between procedure Oriented Programming and Object oriented Programming language Difference between procedure Oriented Programming and Object oriented Programming language 1.)  procedure Oriented Programming Or Structured Oriented (POP):- -> Importance is given to logic.(Algorithm) -> … Read More
  • History of C++ History of C++ -> The C++ language is an object-oriented programming language & is a combination of both low-level & high-level language – a Middle-Level Language. -> C++ was developed by a Danish Computer Sc… Read More

Translate

Popular Posts