Untitled

 avatar
unknown
plain_text
4 days ago
790 B
5
Indexable
#include <iostream>  // For input and output operations
#include <fstream>   // For file handling
#include <string>    // For using the string data type
using namespace std;

int main()
{
    string str;  // Variable to store user input

    ofstream filestream("sample.txt"); // Create and open a file in write mode

    cout << "Enter a text : ";  
    getline(cin, str); // Get a full line of text from the user

    // Check if the file is successfully opened
    if (filestream.is_open()) 
    {
        filestream << str; // Write the user input to the file
        filestream.close(); // Close the file after writing
    }
    else 
        cout << "No Such File created."; // If file creation fails, show an error message

    return 0; // End of program
}
Editor is loading...
Leave a Comment