I have an assignment to output contents from a .txt file to html. I'm able to output the information from the .txt to the console but how would I output it into an html page? The txt and html files are in the same folder as my .cpp file.

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

int main() {
    ofstream outFS;
    ifstream inFS;
    string inputFilename;
    string outputFilename;

    cout<<"Enter file name: " << endl;
    cin>>inputFilename;

    // Opens file
    inFS.open(inputFilename);

    // Check to see if file is in directory
    if (!inFS.is_open()) {
        cout<<"Could not open file " << inputFilename << endl;
        return 1;
    }

    // Reads each line of the file
    while (getline(inFS, inputFilename)) {
        cout<<inputFilename;
    }
    inFS.close();

}

Dani AI

Generated

was right to point out using an output stream and a separate buffer; accidentally reused the filename variable for each line which caused the confusion. To create an HTML file from a .txt file, write a minimal HTML shell (doctype, head with a charset, body) to an ofstream and stream the input lines into it. For plain text the simplest is to put the text inside a <pre> block so line breaks are preserved, and to escape HTML-sensitive characters (&, <, >, quotes) so literal text does not become markup.

A compact example that wraps the text, escapes special characters and writes output.html:

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

static std::string escape_html(const std::string &s) {
    std::string out; out.reserve(s.size());
    for (unsigned char c : s) {
        if (c == '&') out += "&amp;";
        else if (c == '<') out += "&lt;";
        else if (c == '>') out += "&gt;";
        else if (c == '"') out += "&quot;";
        else if (c == '\'') out += "&#39;";
        else out.push_back(c);
    }
    return out;
}

int main() {
    std::string inFile = "input.txt", outFile = "output.html";
    std::ifstream in(inFile);
    std::ofstream out(outFile);
    if (!in || !out) return 1;

    out << "<!doctype html>\n<html>\n<head>\n<meta charset=\"utf-8\">\n<title>"
        << inFile << "</title>\n</head>\n<body><pre>\n";

    std::string line;
    while (std::getline(in, line)) out << escape_html(line) << '\n';

    out << "</pre>\n</body>\n</html>\n";
    return 0;
}

Notes and troubleshooting: confirm the program's working directory or use absolute paths; check file-open error handling; if the text already contains HTML fragments do not escape (or choose a different strategy); replace the <pre> approach with paragraph conversion (double-newline => <p>) for nicer formatting. Opening the generated .html in a browser (double-click or file://) should display the result.

Recommended Answers

All 5 Replies

basically open the ofstream object for output, then use it instead of cout(outFS<<inputFilename;).

From what I understand of <fstream> is that ifstream is if you want to import contents from a file (which I did), and ofstream is when you want to outport contents to a file?

Yes that is what my comment shows. What may be confusing you is that you're using the variable inputFilename to hold each line from the file, instead of a separate variable. See if this clears it up:

string line = "";
outFS.open("OutputFile1.txt", std::ofstream::out)
// Reads each line of the file
while (getline(inFS, line)) 
{
    outFS << line;
}
outFS.close();
inFS.close();

hi,I want a C ++ project that includes all functions


I have yet to find a C++ project with all functions. However you may be in luck as there is a C++ complier testing tool at

That should create a set of projects that would have everything.

PS. You seem to have placed your new question in someone elses thread. Don't do that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.