File Handling using File Streams in C++

File Handling using File Streams in C++

Files are the medium for storing the data and information. We can define the Streams as a sequence of bytes(8bits=1byte). To store the data permanently,  we use the files and use these data to read or write in the form of I/O operations by transferring bytes of data. when we use the File Streams, then We use the header file <fstream>.

Let’s see some other header files related to File Streams.

  • ofstream: ofstream  is the header file used for output Stream and for writing in the files.
  • ifstream: ifstream is the header file used for input Stream and this is used for reading from files.
  • fstream: fstream is the header file used for both output Stream and input Stream. fstream can read from files and also can write to the files.

File Handling Operations:

  • How to create a file?
    •  open()function is used.
  • How to read data?
    • read()function is used.
  • How to write new data?
    • write()function is used.
  • How to close a file?
    • close()function is used.

How to Create/open a File in C++?

The Syntax for file creation in filestreams: FilePointer.open(“Path”,ios::mode);

  • Example: File is opened for writing: T4T.open(“D:\T4Tutorials.txt”,ios::out);
  • Example: File is opened for reading: T4T.open(“D:\T4Tutorials.txt”,ios::in);
  • Example: File is opened for appending: T4T.open(“D:\T4Tutorials”,ios::app);
  • Example: File is opened for truncating: T4T.open(“D:\T4Tutorials”,ios::trunc);

How to Write a File in C++?

How to Read a File in C++?

How to Close a File in C++?

Add a Comment