-1

I want to convert text file into HTML tables but can't create columns.It inputs the whole data into rows.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();
}
Wasi Ahmad
  • 35,739
  • 32
  • 114
  • 161
  • [this is bad](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). Also should `void main` be `int main`. Also fix the formatting of the code – Ed Heal Dec 25 '16 at 05:12
  • You're taking each line from the file, and wrapping it inside a `` with exactly one ``. If you want multiple columns, put each field inside a `` element. What exactly is your question? – Sam Varshavchik Dec 25 '16 at 05:41
  • that's the problem I don't know how to put each field inside a tag.@SamVarshavchik – waheed abbax Dec 25 '16 at 05:49
  • So really you wanted to ask a question about how to split/parse data out of lines from a text file... Perhaps searching for stuff like "c++ how to split lines of text" would yield some useful techniques. – TheUndeadFish Dec 25 '16 at 08:05

1 Answers1

0

It looks like you need to provide tags for each item in a line when you read a line from the file

Currently only 1 tag is added for each row. hence you see only 1 column for the table. To add multiple columns, you need to add multiple tags for column item

You can use a delimiter to split the column items in a line and then loop through the individual elements adding tag

Example:

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

Hope It Helps.

Wasi Ahmad
  • 35,739
  • 32
  • 114
  • 161
Aditya K
  • 466
  • 4
  • 8