Html conversion program

Hello gurus, i have a program that converts document files to html , however there's a very strange issue with the program, it is only converting files that i open within the program using fstream i.e. ofstream out("test.doc"); now ill have to copy the contents from another document that i want to convert to this test file and it would convert but if i try converting a document that has not been implicitly created it would give me a garbage .html file.
Please go through the code, would be grateful if someone can guide me :)

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <iostream>
#include <fstream>
#include <cassert>
#include <string>


#define tab "&nbsp;&nbsp;&nbsp;&nbsp;"

// file extensions that the program can handle
// you can add more extenssions if needed
using namespace std;
char *fileExt[] = {".doc", ".c", ".cpp"};

void convert_to_html(string fileName);

string name;
int main()
{
	/*ofstream o;
	o.open("a.doc");
	o<<"blah blah blah "<<"     "<< "spaced"<<endl<<endl<<"do line";
	o.close();*/
	cout<<"enter name:";
	cin>>name;
	// converting the file entered by the user into an html file
	convert_to_html(name);
cout << "File Conversion Succeeded!";
	return 0;
}

// this is the function that makes the conversion:

void convert_to_html(string fileName)
{
	fstream fin(fileName.c_str(), ios::in);
	if(!fin.is_open())
	{
		cerr << "Error while opening \"" << fileName << "\"" << std::endl;
		assert(fin.good());
	}
	// creating a name for the file that will have the Html Code in it
	string fileName2 = fileName;
	int nExtNum = sizeof(fileExt)/sizeof(fileExt[0]);
	int nPos = -1, nLen = 0;
	for(int i = 0; i < nExtNum; ++i)
	{
		nPos = fileName2.find(fileExt[i]);
		nLen = strlen(fileExt[i]);
		if(nPos != string::npos && (nPos + nLen) == fileName2.length())
		{
			fileName2.erase(nPos, nLen);
			fileName2 += ".html";
			break;
		}
	}
	
	assert(fileName != fileName2);

	fstream fout(fileName2.c_str(), ios::out);
	if(!fin.is_open())
	{
		cerr << "Error while opening \"" << fileName2 << "\"" << endl;
		assert(fout.good());
	}
	fout << "<html>\n";
	fout << "<head>\n";
	fout << "<title>";
	fout << fileName2;
	fout << "</title>\n";
	fout << "</head>\n";
	fout << "<body>\n";
	fout << "<font size = 2>\n";
	char cChar = 0;
	while(fin.get(cChar))
	{
		switch(cChar)
		{
		case '\n':
			fout << "<br>";
			break;
		case '\t':
			fout << tab;
			break;
			case '<':
			fout<< "&lt;";
			break;
		case '>':
			fout<<"&gt;";
			break;
			case '-':
			fout<<"&minus;";
			break;
			case '–':
			fout<<"&ndash;";
			break;
			case '‘':
			fout<<"&lsquo;";
			break;
			case '’':
			fout<<"&rsquo;";
			break;
			case '“':
			fout<<"&ldquo;";
			break;
			case '”':
			fout<<"&rdquo;";
			break;
			case '„':
			fout<<"&bdquo;";
			break;case '‚':
			fout<<"&sbquo;";
			break;case '•':
			fout<<"&bull';";
			break;
		default:
			fout << "&#" << (int)cChar;
		}
	}
	fout << "</font>\n";
	fout << "</body>\n";
	fout << "</html>";
	fin.close();
	fout.flush();
	fout.close();
}


cheers :D
I'm not exactly sure what you're saying. Can you clarify these bits?

it is only converting files that i open within the program using fstream i.e. ofstream out("test.doc");


not been implicitly created


garbage .html file
(Specifically, "garbage" how? Just random noise?)

It would also help if you can give a minimal example of code that doesn't work for you.
Last edited on
Yes, try converting a doc file(say test.doc) to html, it would create html file but with nothing but random numbers and characters; now create a file within the program calling fstream(say inprog.doc), copy-paste the contents from test.doc to inprog.doc and viola, u get a perfect .html with all contents.


Now i cant give a minimal code coz i have no clue where the problem is coming,by far what i see and debugged ; i dont see anything tht can cause this :/
try converting a doc file(say test.doc) to html, it would create html file but with nothing but random numbers and characters
Well, yeah. Your code is merely replacing byte values in the input with HTML tags in the output. You're not actually decoding the .doc format.
Topic archived. No new replies allowed.