Header Responsive Ads

What are Files in C++

Combination of characters, words, sentences and paragraphs is called file.

Types of File

Primarily there are two types of files i.e text file(consist of readable English characters) and executable file.

Text file further categorized into plain text file and formatted text file.

Properties of file

File properties are file name, file size, file creation date, file modification date etc.

We use files and database for permanent storage because program run on RAM which is erased once computer is restarted.

Text file handling

C++ filestream is used to handle text file and for this header file "fstream.h" for C and "fstream" for C++ is used. As cout is used to display program data on screen and cin is used by program to get data from keyboard.

Similarly header file fstream provides two tool ifstream and ofstream.

Ifstream is used by program to read data from a file and ofstream is used by program to write data in file.

Output file handling using ofstream

There are five steps to create text file and write data in it. These steps are:-

Step 1: Include header file fstream

We have already know how to include built in header files in our program as to use cout and cin, we included header file iostream at top of program. Include fstream header file as below.

#include <fstream>

Step 2: Create object of output file stream

Below line is used to create object of ofstream (output file stream)

ofstream ofile;

Here ofile is an object of ofstream and we can use this object to perform all file operations.

Step 3: Create text file and open it

Using dot operator (.), we can call open() method associated with object ofile. This method will open a new text file if text file of this name is already not exist. However, if such file already exists, then this method will only open it to write purpose. Below line is used to do this.

ofile.open(
    "filename.txt",
    File opening mode
);

First parameter of open method is text file name. You may provide complete address of location where you want to create it. If no location address is mentioned, then file will be created at the location same as the program.

Second parameter is file opening mode. It means that for what purpose you are opening the file. We have several options as

  • We can create new file, open it and write in it
  • We can open already created file, open it and read data from it
  • We can open already available file and write in it
  • We can delete old date from file and write in it
  • We can append data with old data in file
  • We can open file and write anywhere in the file

So while opening file, we have to mention that for which purpose we are opening file like open(filename, mode); mode is option. Default mode for output file is ios::out

Syntax of different file opening modes are

  • ios::in         To read data from file
  • ios::out       Erase old data and write in file
  • ios::trunc    Erase old date and write in file
  • ios::app      To append data with old data
  • ios::ate       Write data anywhere in file

Step 4: Write data in file

Below code will write data which is "usa" in the file.

string country="usa";
ofile<<country;

Step 5: Close the file

Don’t forget to close file at the end of program so that resources allocated to ofstream can be de-allocated by the operating system.

Below is a C++ program in which a text file is created, opened and write data in it.

C++ ofstream
C++ ofstream
Output

C++ ofstream output
C++ ofstream output

myfile.txt

C++ ofstream myfile.txt
C++ ofstream myfile.txt

Post a Comment

Previous Post Next Post

In Article Ads 1 Before Post

In Article Ads 2 After Post