2016-12-25 46 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>"; 
} 

希望它有助於。