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);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include<iostream> #include<conio.h> #include <fstream> using namespace std; int main() { fstream T4T; // Step 1: Creating object of fstream class T4T.open("D:\T4Tutorials.txt",ios::out); // Step 2: Creating new file if(!T4T) // Step 3: Checking whether file exist { cout<<"File creation failed"; } else { cout<<"New file created"; T4T.close(); // Step 4: Closing file } getch(); return 0; } |
How to Write a File in C++?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <iostream> #include<conio.h> #include <fstream> using namespace std; int main() { fstream T4T; // Step 1: Creating object of fstream class T4T.open("D:\T4Tutorials.txt",ios::out); // Step 2: Creating new file if(!T4T) // Step 3: Checking whether file exist { cout<<"File creation failed"; } else { cout<<"New file created"; T4T<<"Hello"; // Step 4: Writing to file T4T.close(); // Step 5: Closing file } getch(); return
0; } |
How to Read a File in C++?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include <iostream> #include<conio.h> #include <fstream> using namespace std; int main() { fstream T4T; // step 1: Creating object of fstream class T4T.open("D:\T4Tutorials.txt",ios::in); // Step 2: Creating new file if(!T4T) // Step 3: Checking whether file exist { cout<<"No such file"; } else { char ch; while (!T4T.eof()) { T4T >>ch; // Step 4: Reading from file cout << ch; // Message Read from file } T4T.close(); // Step 5: Closing file } getch(); return 0; } |
How to Close a File in C++?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <iostream> #include<conio.h> #include <fstream> using namespace std; int main() { fstream T4T; // Step 1: Creating object of fstream class T4T.open("D:\T4Tutorials.txt",ios::out); // Step 2: Creating new file T4T.close(); // Step 4: Closing file getch(); return 0; } |