Writing to a File in C++ using fstream

 


In C++, we can write data to a file using the fstream library. The ofstream (output file stream) class is used to create and write to a file.


1️⃣ Steps to Write to a File

  1. Include <fstream> for file handling.
  2. Create an ofstream object to open the file.
  3. Use << operator to write data.
  4. Close the file using .close().

2️⃣ Basic Example: Writing to a File

#include <iostream>
#include <fstream>  // File handling
using namespace std;

int main() {
    ofstream file("example.txt");  // Create/Open file

    if (file.is_open()) {
        file << "Hello, this is a file write example in C++!\n";
        file << "Writing to files is simple!\n";
        file.close();  // Close the file
        cout << "Data written successfully!" << endl;
    } else {
        cout << "Error opening file!" << endl;
    }

    return 0;
}

🔹 Output (example.txt)

Hello, this is a file write example in C++!
Writing to files is simple!

✅ The file "example.txt" is created and written successfully.


3️⃣ Appending to a File (ios::app)

If you want to add data without overwriting existing content, use ios::app.

#include <iostream>
#include <fstream>
using namespace std;

int main() {
    ofstream file("example.txt", ios::app);  // Open file in append mode

    if (file.is_open()) {
        file << "Appending new line!\n";
        file.close();
        cout << "Data appended successfully!" << endl;
    } else {
        cout << "Error opening file!" << endl;
    }

    return 0;
}

✅ This adds data to "example.txt" without erasing previous content.


4️⃣ Writing User Input to a File

#include <iostream>
#include <fstream>
using namespace std;

int main() {
    string text;
    ofstream file("user_data.txt");

    if (file.is_open()) {
        cout << "Enter text to write to file: ";
        getline(cin, text);
        file << text << endl;
        file.close();
        cout << "Data written successfully!" << endl;
    } else {
        cout << "Error opening file!" << endl;
    }

    return 0;
}

🔹 Example Input

Enter text to write to file: Learning C++ file handling!

🔹 Output (user_data.txt)

Learning C++ file handling!

5️⃣ Writing Multiple Lines to a File

#include <iostream>
#include <fstream>
using namespace std;

int main() {
    ofstream file("lines.txt");

    if (file.is_open()) {
        for (int i = 1; i <= 5; i++) {
            file << "This is line " << i << endl;
        }
        file.close();
        cout << "Multiple lines written successfully!" << endl;
    } else {
        cout << "Error opening file!" << endl;
    }

    return 0;
}

🔹 Output (lines.txt)

This is line 1
This is line 2
This is line 3
This is line 4
This is line 5

6️⃣ Summary

ofstream file("filename.txt"); → Create and write to a file.
ios::app → Append data to an existing file.
Always close the file with .close().
Use getline(cin, text) to write user input to a file.

Would you like a file read example next? 📄

Post a Comment

0 Comments