This is going to look like a very basic question to allot of you but for the life of me I can't find how to do this anywhere on the web. Allot of convoluted solutionsn out there.

I simply want to save a textfile as a .html file. Having seriouse difficulties.
In the below code, towards the bottom I want to save the file as an html file.

Any suggestions. Thanks

#include <fstream>
#include <iostream>

int main()

    std::ofstream FileOne("c:\\testfile.txt");

        std::cerr<<"Cannot open the output file."<<std::endl;
        return 1;

    FileOne.close();            // How do I save FileOne "testfile.txt" as testfile.html ?
    return 0;

Recommended Answers

All 9 Replies

One quick way is to use an html editor. Paste the text into a new page. then look at the source. from here you can see the type of code you'll need to prepend and append to the text, in order to render it as html.

What I'm trying to do is save a file as an HTML file using C++ code, nothing more.
Not sure how the above relates to this but doing it manually is not an option.

Maybe I'm just not understanding your reply.


Sorry I assumed you at least know what html is. Perhaps you should explain your question more clearly.

Does the textfile already contain html code?

just change the extension to .htm or .html

Or is it just some text you would like to display in a web page?

You will need to add code before the text(prepend) and code after the text(append) then save it all together, with the .htm or .html extension.

Sorry I assumed you at least know what html is. Perhaps you should explain your question more clearly.

I don't know how clear I can be. I've provided the code I'm using as a very basic example of what I'm trying to do. I don't want to put HTML in my textfile just yet. I'm just starting with the basics, and that is saving an open text file as an HTML file. Yes, I can put .html at the end of the text document but I want to do this in my C++ code. I don't want to add anything to the file yet, thats the easy part.

Please don't look into it too much.

//I open the file (Works Fine by the way)
std::ofstream FileOne("c:\\testfile.txt");

//Now I want to save the file as an HTML file. Nothing more.  Can I use 'SaveAs' ?
I can't.  Then what can I use to save testfile.txt as testfile.html?

An html file is plain text.

std::ofstream FileOne("c:\\testfile.html");

This creates an empty html file.

Assuming that you are creating a new file, then the simplest solution is just to name the file testfile.html to begin with:

std::ofstream FileOne("c:\\testfile.html");

The C++ file I/O works the same no matter what the file extension is; a 'text file' is just a stream of bytes which is interpreted as character data, after all.

OTOH, if there is an existing file named testfile.txt already, and you want to rename it to testfile.html, then the easiest solution is to open testfile.txt for reading, and testfile.html for writing, and copy the data from the former to the latter:

std::ifstream infile("c:\\testfile.txt");
std::ofstream outfile("c:\\testfile.html");
std::string temp;

while (infile.good() && outfile.good())
    getline(infile, temp);
    outfile << temp;

If you absolutely need to rename the file in place, then you need to use the <cstdio> function rename():

int result = rename("C:\\testfile.txt", "C:\\testfile.html");

I hope that one of these is what you are looking for.

commented: Great comprehensive answer +3

Schol-R-LEA - Thanks very much.. I think thats what I need.
Also thanks Moschops.
Well, I didn't know you would write directly to an HTML file like that, though I've never tried.

So I'm assuming there is no way possible to save a text file as an HTML file as you would manually?

I instead have to have an existing copy of a textfile and an html file and copy the contents in between. The reason I am trying to do this is becuase I have 3850 textfiles at work I need to convert to html. These are reports written in html format to be saved as html and can be viewed in a web browser. I could do them manually but I want to write this program to do this. This is why I wanted to save each one as HTML. It appears this is impossible to do in C++. I'll just have to loop through each file and copy it's contents to an existing HTML file. Am I correct in assuming this?

So I'm assuming there is no way possible to save a text file as an HTML file as you would manually?

You do it exactly the same way.

Am I correct in assuming this?

No. Use rename, as Schol-R-LEA suggested at the end of his post.

Brillaint. thanks

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.