Call us at 0700-922-6559 or Click here

Write a program to read and write from file.

#include <iostream>

#include <fstream>

using namespace std;

int main() {

   // Write to a file

   ofstream outfile(“myfile.txt”);  // Create an output file stream

   if (outfile.is_open()) {

       outfile << “Hello, world!\n”;

       outfile << “This is some text written to a file.\n”;

       outfile.close();  // Close the file stream

       cout << “Data written to file successfully.” << endl;

   } else {

       cout << “Error opening file for writing.” << endl;

   }

   // Read from the file

   ifstream infile(“myfile.txt”);  // Create an input file stream

   if (infile.is_open()) {

       string line;

       while (getline(infile, line)) {

           cout << line << endl;

       }

       infile.close();  // Close the file stream

   } else {

       cout << “Error opening file for reading.” << endl;

   }

   return 0;

}

Output:

Data written to file successfully.

Hello, world!

This is some text written to a file.

Here’s a step-by-step explanation of the code:

1. Headers and Namespace:

  • #include <iostream>: Includes the iostream header for input/output operations.
  • #include <fstream>: Includes the fstream header for file input/output operations.
  • using namespace std;: Brings the std namespace into scope for convenient use of elements like cout, cin, ofstream, ifstream, etc.

2. Writing to a File:

  • ofstream outfile(“myfile.txt”);: Creates an output file stream object named outfile and associates it with the file “myfile.txt”.
  • if (outfile.is_open()) { … }: Checks if the file was opened successfully.
    • outfile << “Hello, world!\n”;: Writes the string “Hello, world!” to the file, followed by a newline character.
    • outfile << “This is some text written to a file.\n”;: Writes another string to the file.
    • outfile.close();: Closes the file stream to ensure data is written and resources are released.

3. Reading from the File:

  • ifstream infile(“myfile.txt”);: Creates an input file stream object named infile to read from “myfile.txt”.
  • if (infile.is_open()) { … }: Checks if the file was opened successfully for reading.
    • string line;: Declares a string variable to store lines read from the file.
    • while (getline(infile, line)) { … }: Reads lines from the file using getline() until the end of the file is reached.
      • cout << line << endl;: Prints each line to the console.
    • infile.close();: Closes the file stream when reading is finished.

4. Key Points:

  • File Streams: The fstream library provides file stream objects for input (ifstream) and output (ofstream) operations.
  • Opening and Closing Files: Use the open() function to create a file stream and associate it with a file, and use close() to release resources.
  • Error Handling: Always check if files were opened successfully using is_open() to handle potential errors.
  • Writing to Files: Use the insertion operator (<<) to write data to a file stream.
  • Reading from Files: Use the getline() function to read lines from a file stream.

Leave a Reply

Your email address will not be published. Required fields are marked *