2016-12-25 110 views
-1

我想将文本文件转换为HTML表格,但无法创建列。它将整个数据输入到行中。 As you can see in the picture there are no separate columns for each section.使用C++将txt文件转换为html表格

#include<iostream> 
#include<fstream> 
#include<string> 
#include<conio.h> 
using namespace std; 
void main() 
{ 
    ifstream x; 
    string name; 
    string head = "<!DOCTYPE html>\n<html>\n<head> <style> table, th, td { border: 1px solid black; } </style> </head><body>\n<table>"; 
    string tail = "</table></body>\n</html>"; 
    string bodystart = "<tr><td>\n"; 
    string bodyclose = "</td></tr>"; 

    ofstream y; 
    x.open("example.txt",std::ios::app); 
    y.open("myhtmlfile.html"); 

    y << head; 
    while (getline(x, name)){ 
     y << bodystart << name <<bodyclose; 
    } 
    y << tail; 
    x.close(); 
} 
+0

[this is bad](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)。也应该'void main'是'int main'。还要修复代码的格式 –

+0

您正在从文件中取出每一行,并将其封装在一个“”中,并且只包含一个“​​”。如果您想要多列,请将每个字段放入'​​'元素中。你的问题到底是什么? –

+0

这就是问题我不知道如何把每个字段放在一个​​标签里面@ SamVarshavchik –

回答

0

它看起来像你需要为每个项目,当您从

目前只有1个标签为每个行添加的文件中读取一行提供标签在一条线上。因此您只能看到1列的表格。要添加多列,则需要添加多个标签栏项目

您可以使用分隔符来拆分,通过各个元素在一行中的列项,然后环添加标签

例子:

while (getline(x, name)){ 
    y << "<tr>"; 
    for(int i=0;i<noofcolumns;i++) { 
     y << "<td>"; 
     // y << columnItem; 
     y << "</td>"; 
    } 
    y << "</tr>"; 
} 

希望它有助于。