[try Beta version]
Not logged in

 
how to create htm file?

Aug 19, 2010 at 2:37am
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?
Aug 19, 2010 at 2:38am
I find it will automatically combin two lines content to one line in htm file.
Can you post some code?
Aug 19, 2010 at 4:28am
good morning
Last edited on Aug 26, 2010 at 5:35am
Aug 19, 2010 at 4:32am
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.
Aug 19, 2010 at 4:56am
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 Aug 22, 2010 at 5:11am
Aug 23, 2010 at 1:40am
is there some who knows how to do?
Aug 23, 2010 at 2:00am
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;
Aug 23, 2010 at 4:18am
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.
Aug 23, 2010 at 4:45am
You need to escape some characters.
See here for a list:
http://www.theukwebdesigncompany.com/articles/entity-escape-characters.php
Aug 23, 2010 at 5:19am
I hvae substitude all \ " in the code for &quot. and later substitude \ " for &#34.

but there is no effect.

Last edited on Aug 23, 2010 at 5:21am
Aug 26, 2010 at 5:36am
good afternoon every one.
Aug 26, 2010 at 9:38am
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.
Aug 27, 2010 at 2:25pm
when I get rid of *.it work well.thanks a lot.
Topic archived. No new replies allowed.