Delimiter not working

For a project I have to pass data from a file into an HTML code. The delimiter is not working (\t) instead of selecting all data between tabs, it selects only some data in between tabs and messes up the whole form. Any help would be appreciated.
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


#include <iostream>
#include <fstream>
using namespace std;

   string Cnumber ="Course Number";
   string Cname ="Course Name";
   string date1 ="Date2";
   string date2 ="Date1";
   string times ="Times";
   string Dnumber ="Number of Days"; 

void head();
void table();
void foot();

int main()
{
  ifstream fin;
   ofstream fout;
   
      
   
   fin.open("Schedule.txt");
   fout.open("site.html");
   
   fout << "<DOCTYPE! HTML>" << endl;
   fout << "<html>" << endl;
   fout << "\t" << "<head>" << endl;
   fout << "\t" << "</head>" << endl;
   fout << "\t" << "<body>" << endl;
   fout << "\t" << "\t" << "<table border = '2' bordercolor = 'black' bgcolor = 'lightblue'>>" << endl;
   fout << "\t" << "\t" << "\t" << endl;
   fout << "\t" << "\t" << "\t" << "<tr>" << endl;

   fout << "\t" << "\t" << "\t" << "<td>" << "Course Number" << "</td>" << endl;
   fout << "\t" << "\t" << "\t" << "<td>" << "Course Name" << "</td>" << endl;
   fout << "\t" << "\t" << "\t" << "<td>" << "Start" << "</td>" << endl;
   fout << "\t" << "\t" << "\t" << "<td>" << "Finish" << "</td>" << endl;
   fout << "\t" << "\t" << "\t" << "<td>" << "Times" << "</td>" << endl;
   fout << "\t" << "\t" << "\t" << "<td>" << "Number of Days" << "</td>" << endl;
   while(!fin.eof())
   {
      getline(fin, Cnumber, '\t');
      fout << "\t" << "\t" << "\t" << "<tr>" << endl;
      fout << "\t" << "\t" << "\t" << "<td>" << Cnumber << "</td>";
      getline(fin, Cname, '\t');
      fout << "\t" << "\t" << "\t" << "<td>" << Cname << "</td>";
      getline(fin, date1, '\t');
      fout << "\t" << "\t" << "\t" << "<td>" << date1 << "</td>";
      getline(fin, date2, '\t');
      fout << "\t" << "\t" << "\t" << "<td>" << date2 << "</td>";
      getline(fin, times, '\t');
      fout << "\t" << "\t" << "\t" << "<td>" << times << "</td>";
      getline(fin, Dnumber, '\t');
      fout << "\t" << "\t" << "\t" << "<td>" << Dnumber << "</td>"<< endl;


   }
   fout << "\t" << "\t" << "\t" << "</tr>" << endl;
   fout << "\t" << "\t" << "</table>" << endl;
   fout << "\t" << "</body>" << endl;
   fout << "</html>" << endl;


   
   return 0;
}

Hello aaronpeart,

It ould help if you include some or all, if small, so everyone knows what you are using and can test your program.

For me it is late, so I will have to dig into the program tomorrow morning.

I have written a program similar to this which might help

Andy
Hey Andy, this is due tonight so unfortunately that wont help much. I would still like to hear what you have to say. But here is two lines from the .txt file

1515C Cisco Implementing Video Network Devices 2 3/14/2016 3/18/2016 8:30 AM 4:30 PM Eastern Time 5
5384G Deploying Cisco Basic and Advanced Wireless LANs v1.1 3/14/2016 3/18/2016 8:30 AM 4:30 PM Eastern Time 5
Your last getline() should probably use the default delimiter.

I'm only guessing because the file sample you posted doesn't have any tab characters since you failed to put the lines into code blocks to preserve the special characters.


Hello aaronpeart,

Sorry I could not do much before your deadline.

Just to help you in the future.

Your header files is missing <string> and I added <iomanip>.

Lines 7 - 12 should be inside main and you should avoid using global variables unless they start with "constexpr" or "const" as these can not be changed.

Your prototypes are good and I think I see their use.

After you open the input file you need to check that it is open and in good order. The output does not need any verification because for an output file if the file does not exist it creates the file.

The first bit of output code, I think this might be the "head" function, I used the "setw()" to space the tags. The "setw"s are not perfect and still need some adjustment.

Line 43. This is not the best way to read a file.

while(!inFile.eof())

I do not know why people teach this. This is a bad idea as it does not work the way you think it will. Generally by the time the while condition determines that "eof" has been reached you will have processed the last read twice and wondering why it shows up twice.

What happens is the while condition is checked and “eof” has not been set yet. After entering the while loop you read the last line or bit of information from the file process the information reach the bottom of the while loop. At this point “eof” has not been set yet and you return to the while condition which is still good. After entering the loop you try to read from the file, but now there is nothing left to read and “eof” is set, but you do nothing at this point to deal with the “eof” condition. So you process what information is in the variables from the last read before returning to the while condition where it finally detects the “eof”, but you have processed the last good read twice unless you have cleared the variables at the end of the while loop in which case the last process will be blank.
A more acceptable way of using the while loop is:

1
2
3
4
5
while (infile >> someVariable)
{
    infile >> additionalVariables;
    //  Additional code.
}


In the above example you can set up the while condition to read one variable, two or more variables or use “std::getline(?, str)” where ? could be from “std::cin” or from a file stream and “str” represents any “std::string” variable.

As you will in the while loop I added some "endl"s and for the last "getline" I removed the delimiter of "\t" because this is reading the end of the line which ends with "\n" not "\t". The default delimiter for "getline" is "\n". If I understood correctly this is reading the information into the proper variables.

After I adjusted the program I opened the output file in my browser and it made a nice table.

In the end it really does not make any difference how the output file is indented as long as the tags are correct.

For what it is worth when I did something like this I first started by writing the "html" code to get an idea of how to write the C++ code. In the end I divided up the "html" code into three sections. The first section included the lines "<DOCTYPE! HTML>" to the opening "<body>" tag. This mad it easy to read the input file and write it to the output file. The third section finished up the "html" code that would not change all the way down to the closing "html" tag.

For the second section this is what the program would put together based on reading a "csv" file that created most of the table.

After processing the second section and the doing the third section my program would finish with writing a bit of JavaScript at the end.

Next time do not wait until the end to ask a question. Give your self more time for the questions and answers.

This is the code I changed this morning. It is not perfect, but should be enough for ideas and what should be done. Be sure to read the comments I put in the code.

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
#include <iostream>
#include <iomanip>  // <--- Added.
#include <string>   // <--- Added.
#include <fstream>

//using namespace std;  // <--- Best not to use.

std::string Cnumber = "Course Number";
std::string Cname = "Course Name";
std::string date1 = "Date2";
std::string date2 = "Date1";
std::string times = "Times";
std::string Dnumber = "Number of Days";

// I think I have an idea what these are used for and it is a good idea. I used files for this when I did
// my program.

void head();
void table();
void foot();

int main()
{
	std::ifstream fin;
	std::ofstream fout;

	fin.open("Schedule.txt");
	// <--- You should check that the input file is open.

	fout.open("site.html");

	fout << "<DOCTYPE! HTML>" << std::endl;
	fout << "<html>" << std::endl;
	fout << std::setw(4) << "<head>" << std::endl;  // <--- The "setw()" makes it easier to keep track of the indenting.
	fout << std::setw(4) << "</head>" << std::endl;
	fout << std::setw(4) << "<body>" << std::endl;
	fout << std::setw(8) << "<table border = \"2\" bordercolor = \"black\" bgcolor = \"lightblue\">" << std::endl;  // <--- I believe the attributes should be in double quotes not single.
	fout << std::setw(12) << std::endl;
	fout << std::setw(12) << "<tr>" << std::endl;

	fout << "\t" << "\t" << "\t" << "<td>" << "Course Number" << "</td>" << std::endl;
	fout << "\t" << "\t" << "\t" << "<td>" << "Course Name" << "</td>" << std::endl;
	fout << "\t" << "\t" << "\t" << "<td>" << "Start" << "</td>" << std::endl;
	fout << "\t" << "\t" << "\t" << "<td>" << "Finish" << "</td>" << std::endl;
	fout << "\t" << "\t" << "\t" << "<td>" << "Times" << "</td>" << std::endl;
	fout << "\t" << "\t" << "\t" << "<td>" << "Number of Days" << "</td>" << std::endl;
	fout << std::setw(12) << "</tr>" << std::endl;

	while (std::getline(fin, Cnumber, '\t'))  // <--- Better way than "eof".
	{
		fout << std::setw(12) << "<tr>" << std::endl;
		fout << "\t" << "\t" << "\t" << "<td>" << Cnumber << "</td>" << std::endl;  // <--- Added the "endl".
		std::getline(fin, Cname, '\t');
		fout << "\t" << "\t" << "\t" << "<td>" << Cname << "</td>" << std::endl;  // <--- Added the "endl".
		std::getline(fin, date1, '\t');
		fout << "\t" << "\t" << "\t" << "<td>" << date1 << "</td>" << std::endl;  // <--- Added the "endl".
		std::getline(fin, date2, '\t');
		fout << "\t" << "\t" << "\t" << "<td>" << date2 << "</td>" << std::endl;  // <--- Added the "endl".
		std::getline(fin, times, '\t');
		fout << "\t" << "\t" << "\t" << "<td>" << times << "</td>" << std::endl;  // <--- Added the "endl".
		std::getline(fin, Dnumber);  // <--- Removed the delimiter. The default is '\n' which you need here.
		fout << "\t" << "\t" << "\t" << "<td>" << Dnumber << "</td>" << std::endl;
		std::cout << std::endl;
	}

	fout << std::setw(12) << "</tr>" << std::endl;
	fout << "\t" << "\t" << "</table>" << std::endl;
	fout << "\t" << "</body>" << std::endl;
	fout << "</html>" << std::endl;

	return 0;
}


Almost forgot. You are missing some closing "tr" tags. Although this is not absolutely necessary it is good form and programming to include them.

Hope that gives you some ideas for the future,

Andy
Hello aaronpeart,

What I did forget to mention is that I changed the input file changing some of the spaces to tabs, so the program would be able to read it. There is no point in posting the changes as you would not be able to tell the difference between spacea and a tab.

For the future a "csv" (comma separated value) file would make it easier to read a file.
A "csv" file would look like:

1515C,Cisco Implementing Video Network Devices 2,3/14/2016,3/18/2016,8:30 AM 4:30 PM Eastern Time,5
5384G,Deploying Cisco Basic and Advanced Wireless LANs v1.1,3/14/2016,3/18/2016,8:30 AM 4:30 PM Eastern Time,5

Because of the comma there is no need for a space after the comma.

Hope that helps,

Andy
Hello aaronpeart,

While I was playing with your program I realized that I was a bit off when using the 'setw"s. The "std::setw(4)" part is correct. I was thinking to far ahead and missed that the intent was to print 4 spaces or actually three spaces and something for a total length of four. So if the character you want to print is a space you would end up with four spaces the proper code should be:
fout << std::setw(4) << ' ' << "<head>" << std::endl;. Notice the single space between the "setw" and "head". this will expand to four total spaces.

This line fout << std::setw(12) << ' ' << "</tr>" << std::endl; was after the while loop and I moved it into the while loop.

A couple more changes and this makes a very nice output of a "html" file.

Hope that helps,

Andy
Topic archived. No new replies allowed.