// © Copyrights 2026 Praajna Pattada

#include <iostream>
#include <fstream>
#include <string>
#include <limits>

int ReadFromFile(std::string fileName)
{
    std::ifstream inf{ fileName };
    if (!inf)
    {
        // Display an error and exit
        std::cerr << "The file " << fileName << " cannot be opened for viewing.\n";
        return 0; // We use a custom return value to indicate success or failure of the function. In this case, 1 indicates success and 0 indicates failure.
    }

    std::string Input{};
    // Executes as long as there are lines to read in the file
    while (std::getline(inf, Input)) // You can use 'inf >> Input' as the condition, to read word by word, but using 'std::getline(inf, Input)' allows you to read the file line by line, which is often more useful for text files.
    {
        std::cout << Input << '\n';
    }
    
    return 1;
}

int WriteToFile(std::string fileName)
{
    std::ofstream outf{ fileName };

    // If the output file stream cannot be opened for writing
    if (!outf)
    {
        // Display an error and exit
        std::cerr << "The file " << fileName << " cannot be opened for writing.\n";
        return 0;
    }
    
    // Write some text to the file
    outf << "Hello, world!\n";
    outf << "This file has been written using a C++ program.\n";
    outf << "If you can see this, it means that the program has successfully written data to this file.\n";
    return 1;
}

int main()
{
    std::string Filename {};
    std::string UserInput {};
    char selection {};
    int status {};
    
    do {
        std::cout << "What would you like to do? Enter a number to select the operation:" << '\n' << '\n';
        std::cout << "1. Read from file" << '\n';
        std::cout << "2. Write to file" << '\n';
        std::cout << "3. Exit" << '\n' << '\n';
        std::getline(std::cin >> std::ws, UserInput);
        selection = UserInput[0];
        
        if (selection == '1')
        {
            std::cout << "Enter the filename: ";
            std::getline(std::cin >> std::ws, Filename);
            status = ReadFromFile(Filename);

            if (status == 1)
            {
                std::cout << '\n' << '\n' << "File read successfully." << '\n';
            }

            std::cout << "__________" << '\n' << '\n';
        } else if (selection == '2')
        {
            std::cout << "Enter the filename: ";
            std::getline(std::cin >> std::ws, Filename);
            status = WriteToFile(Filename);

            if (status == 1)
            {
                std::cout << "File written successfully." << '\n';
            }

            std::cout << "__________" << '\n' << '\n';
        } else if (selection == '3')
        {
            std::cout << "Exiting program..." << '\n';
            std::cout << "__________" << '\n';
            continue;
        } else
        {
            std::cout << "Invalid selection. Please try again." << '\n';
            std::cout << "__________" << '\n' << '\n';
        }
    } while (selection != '3');
    
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    std::cin.get();
    return 0;
}
