2016-12-25 12 views
-1

テキストファイルをHTMLテーブルに変換したいが、列を作成できません。データをすべて行に入力します。あなたはC++を使用してtxtファイルをhtmlテーブルに変換

は、現在唯一の1のタグが各行に追加されたファイルから行を読み込むときに、ライン内の各項目のタグを提供する必要があるように見えますAs you can see in the picture there are no separate columns for each section.

#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

[これは悪い](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)また 'void main'は' int main'であるべきです。また、コードの書式を修正してください –

+0

ファイルから各行を取り出して、それを '​​'で ''の中にラップしています。複数の列が必要な場合は、各フィールドを '​​'要素の中に入れます。あなたの質問は何ですか? –

+0

それは私が各フィールドを​​タグの中に入れる方法を知らないという問題です。@ SamVarshavchik –

答えて

0

。したがって、テーブルの列は1つしか表示されません。

while (getline(x, name)){ 
    y << "<tr>"; 
    for(int i=0;i<noofcolumns;i++) { 
     y << "<td>"; 
     // y << columnItem; 
     y << "</td>"; 
    } 
    y << "</tr>"; 
} 
:複数の列を追加するには、あなたが行の列の項目を分割して、タグ

例を追加する個々の要素をループする区切り文字を使用することができます列項目

ために複数のタグを追加する必要があります

希望はそれを助けます。

関連する問題