NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

( 13 )

OBJECTIVE: TO ILLUSTRATE THE CONCEPT OF IFSTREAM, OFSTREAM AND FSTREAM IN C++.

THEORY:
In C++, ifstream, ofstream, and fstream are classes for handling file I/O. They are part of the C++ Standard Library's iostream library and are used for reading and writing files.

Ifstream:
In C++, ifstream (input file stream) is a class provided by the Standard Library that is used for reading data from files. It is commonly used when we need to read information from a file, such as text or binary data.
Here's how we can use ifstream in C++:
a. Include the necessary header: We need to include the <fstream> header to use ifstream.

#include <fstream>

b. Create an ifstream object: We can create an ifstream object and associate it with a file by specifying the file's name in the constructor.

ifstream inputFile("example.txt");

In this example, "example.txt" is the name of the file we want to read from.

c. Check if the file is open: It's a good practice to check if the file was opened successfully before attempting to read from it.

if (inputFile.is_open()) {
// File is open, perform read operations here.
} else {
// File failed to open, handle the error.
}
d. Read data from the file: We can use various methods to read data from the file, such as >> (the extraction operator) for reading values like integers or strings, or getline() for reading entire lines of text.
int num; inputFile >> num; // Read an integer from the file string line; getline(inputFile, line); // Read a line of text from file
e. Close the file: After you've finished reading from the file, it's a good practice to close it.
inputFile.close();

Here's a complete example of using ifstream to read data from a file:
#include <iostream> #include <fstream> using namespace std; int main() {
ifstream inputFile("example.txt"); if (inputFile.is_open()) { int num; inputFile >> num;
cout << "Read integer from file: " << num << endl; string line;
getline(inputFile, line);
cout << "Read a line from file: " << line << endl; inputFile.close();
} else {
cout << "Error opening the file." << endl;
} return 0;
}

In this example, the program opens the file "example.txt," reads an integer and a line of text from it, and then closes the file.





ofstream:
In C++, ofstream (output file stream) is a class provided by the Standard Library that is used for writing data to files. It is commonly used when we need to create or overwrite a file and write information to it.
Here's how we can use ofstream in C++:

a. Include the necessary header: We need to include the <fstream> header to use ofstream.
#include <fstream>

b. Create an ofstream object: We can create an ofstream object and associate it with a file by specifying the file's name in the constructor. If the file already exists, it will be overwritten by default. If it doesn't exist, a new file will be created.
std::ofstream outputFile("example.txt");
In this example, "example.txt" is the name of the file you want to write to.

c. Check if the file is open: It's a good practice to check if the file was opened successfully before attempting to write to it.

if (outputFile.is_open()) {
// File is open, perform write operations here.
} else {
// File failed to open, handle the error.
}
d. Write data to the file: We can use the << (insertion operator) to write various types of data to the file.

int num = 42; outputFile << "This is a number: " << num << std::endl; std::string text = "Hello, world!"; outputFile << text;

We can perform more complex write operations depending on your specific requirements.

e. Close the file: After we have finished writing to the file, it's a good practice to close it.
outputFile.close();
Here's a complete example of using ofstream to create or overwrite a file and write data to it:
#include <iostream> #include <fstream> using namespace std; int main() { ofstream outputFile("example.txt"); if (outputFile.is_open()) { int num = 42;
outputFile << "This is a number: " << num << endl;
string text = "Hello, world!";
outputFile << text; outputFile.close();
cout << "Check the example.txt file. << endl;
} else {
cout << "Error opening the file." << endl;
} return 0;
}


fstream:
In C++, the fstream class is a part of the Standard Library and is used for handling file input and output operations. The name "fstream" is a combination of "file" and "stream," indicating that it is a class that can be used to work with files as if they were input or output streams. The fstream class is derived from both ifstream and ofstream, which means it can be used for both reading from and writing to files.

Example:

#include <iostream> #include <fstream> using namespace std;
int main() {
// create object of fstream class with input and output mode.
fstream file("example.txt");

// check if file object is open for desired mode

if (file.is_open()) { int num;
file >> num;// read a line from file into num variable cout << "Read from file: " << num << endl;

// Clear the stream and get ready for writing file.clear();

// write text into output stream file << "nThis is a line of text." << endl; cout << "Wrote to file." << endl; file.close();

} else {
cout << "Error opening the file." << endl;
} return 0;
}

CONCLUSION:
Hence, C++ program to demonstrate ifstream, ofstream and fstream was written,executed and compiled using Dev C++ ID.

     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.