Open Current Source File

 avatar
tuhuuduc
c_cpp
22 days ago
518 B
3
Indexable
Never
#include <iostream>
#include <fstream>
#include <string>

int main() {
    std::ifstream file(__FILE__); // Open the current source file
    std::string line;

    if (file.is_open()) {
        // Read and print each line of the source file
        while (std::getline(file, line)) {
            std::cout << line << std::endl;
        }
        file.close(); // Close the file
    } else {
        std::cerr << "Failed to open source file." << std::endl;
        return 1;
    }

    return 0;
}
Leave a Comment