how to create htm file?

I want to convert a txt file to htm file.

but when I use ofstream ofs("*.htm").

I find it will automatically combin two lines content to one line in htm file.

why does it happen,and any other way to creat a htm file?
I find it will automatically combin two lines content to one line in htm file.
Can you post some code?
good morning
Last edited on
In response to your second question, you can escape the desired characters:
1
2
string s = "This string \"has\" quotes.";
cout << s;

...prints:
This string "has" quotes.
thank you toolbox.below is my code.but I can't get right answer.can you help me to find what is wrong with it?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <string>
#include <fstream>
typedef unsigned int uint;
using namespace std;

void replace()
{ 
   string strline,s;
   ifstream ifs("sft004.txt");
   
   const char * head="<html><head>\n\r<meta HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; CHARSET=UTF-8\"><meta name=\"DESCRIPTION\" CONTENT=\" Serial Monitor HTML file\"><meta name=\"GENERATOR\" CONTENT=\"Serial Monitor\"><meta name=\"COPYRIGHT\" content=\"Micro software\"><meta name=\"Author\" CONTENT=\" Serial Monitor\"><title>t Serial Monitor HTML Export</title></head>";
   const char * lineodd="<P><FONT face=\"Courier New\" color=\"#0000ff\"><B>";
   const char * lineeven="<P><FONT face=\"Courier New\" color=\"#ff00ff\"><B>";
   const char * lineend="</B></P>";
   const char * body="<body><!--StartFragment --><PRE><CODE>";
   const char * end="</CODE></PRE></body></html>";
   
	char Temp16[4]={0};
	char buffer[5]={0};
	
    int  strlen,i=0;
      s+=*head;
      s+=*body;
	while(getline(ifs,strline))
    {   i++;
	           if(i%2==0)
               s+=*lineeven+strline+*lineend+"\n\r";
               s+=*lineodd+strline+*lineend+"\n\r";
               
          }
      s+=*end;
 	ofstream ofs("sft004.html");
     ofs<<s;
    ifs.close();
    ofs.close();

 }
int main(int argc, char* argv[])
{
    replace();    cout << "replace finished" << endl;
	cin.get(); 
	return 0;
}
Last edited on
is there some who knows how to do?
Well, what are the remaining problems with your code? (other than the formatting looking like a bomb exploded in there)
If the source file contains any quotation marks, you'll have to replace them with &quot;
what is the meanning of "&quot"you refer to?

the code can be compiled.but the created htm file,is wrong ,there are much ’ <‘ in every line .

and the color is black and white.
You need to escape some characters.
See here for a list:
http://www.theukwebdesigncompany.com/articles/entity-escape-characters.php
I hvae substitude all \ " in the code for &quot. and later substitude \ " for &#34.

but there is no effect.

Last edited on
good afternoon every one.
Have you run this in a debugger? The problem would be obvious as you stepped thru the code.

What does this do?
s+=*head;

How is it different from:
s+=head;

If you fix the indentation, it will be obvious that lineeven and lineodd are not being used correctly.
when I get rid of *.it work well.thanks a lot.
Topic archived. No new replies allowed.