Open Current Source File

 avatar
tuhuuduc
c_cpp
a year ago
518 B
8
Indexable
#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;
}
Editor is loading...
Leave a Comment