Skip to content

Instantly share code, notes, and snippets.

Created April 7, 2013 21:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/5332564 to your computer and use it in GitHub Desktop.
Save anonymous/5332564 to your computer and use it in GitHub Desktop.
Here is the code I have for a certain project. The problem I am having mostly is concerned with the void function at the bottom of the code. It is a function that writes the body of an HTML document that can be accessed in a browser. Right now it consists of a table with 2 columns labeled "KEY" and "VALUE" respectively. The way the code has it, …
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <string>
#define ARRAY_SIZE 256
using namespace std;
void initializeArray(int nArray [], int nArraySize);
void outputCSV(int nArray [], int nArraySize, ofstream& fCSV);
void outputHTML(int nArray [], int nArraySize, ofstream& fHTML);
void writeHeader (ofstream& ofHtmlFile);
void writeBody (ofstream& ofHtmlFile, int nArray [], int nArraySize);
void writeFooter ( ofstream& ofHtmlFile);
int main (int argc, char *argv[] )
{
ifstream fin;
ofstream fHTML;
ofstream fCSV;
string sInputFileName="";
char cInput=' ';
int nInput=0;
int nArray[ARRAY_SIZE];
for (int i=0; i<argc; i++)
{
string sTemp="";
cout<<"argument " << i << "=" << argv[i] << endl;
sTemp=argv[i];
if (sTemp.compare("-f")==0)
{
sInputFileName=argv[i+1];
cout << "The input file name is " << sInputFileName << endl;
}
}
/*fin.open(sInputFileName);
if (fin.fail())
{ //check for sucesfful file opening for reading
cout << "Input file opening failed" << endl;
exit(1);
}*/
fHTML.open ("project4.html");
if (fHTML.fail())
{ //check for sucesfful file opening for writing
cout << "Failure opening output file project4.html" << endl;
exit(1);
}
fCSV.open("project4.csv");
if (fCSV.fail())
{ //check for sucesfful file opening for writing
cout << "Failure opening output file project4.csv" << endl;
exit(1);
}
initializeArray(nArray, ARRAY_SIZE); //initialize all members of the array to 0
while (!fin.eof()) //loop until we reach the end of the input file
{
char cInput=fin.get(); //get the character from the file
int nInput=(int)cInput; //convert to integer
if ((0 < nInput) && (nInput < ARRAY_SIZE))
{
++nArray[nInput]; //increment the counter
}
}
fin.close( ); //closes the input file
outputCSV(nArray, ARRAY_SIZE, fCSV);
outputHTML(nArray, ARRAY_SIZE, fHTML);
fHTML.close( ); //closes the HTML output file
fCSV.close( ); // closes the CSV output file
cin.get();
return 0;
}
void initializeArray(int nArray[], int nArraySize)
{
for (int i=0; i<nArraySize; i++) //loop through all members of the array
{
nArray [i]=0; // set the ith member to 0
}
return;
}
void outputCSV(int nArray[], int nArraySize, ofstream& fCSV)
{
for (int i=0; i<nArraySize; i++)
{
fCSV << i; //output the value of i
fCSV.put (','); //separates each value with a comma
}
fCSV.put('\n'); //end of the first line (the header record)
for (int i=0; i<nArraySize; i++)
{
fCSV << nArray[i];
fCSV.put(',');
}
fCSV.put('\n');
return;
}
void outputHTML(int nArray [], int nArraySize, ofstream& fHTML)
{
ofstream ofHtmlFile;
cout << "Start of program." << endl;
ofHtmlFile.open("project4.html");
if (ofHtmlFile.fail ())
{
cout << "Open project4.html failed." << endl;
exit (1);
}
writeHeader(ofHtmlFile);
writeBody(ofHtmlFile, nArray, nArraySize);
writeFooter(ofHtmlFile);
ofHtmlFile.close ();
cout << "End of program." << endl;
return;
}
void writeHeader( ofstream& ofHtmlFile)
{
ofHtmlFile << "<HTML>" << endl;
ofHtmlFile << "<HEAD>" << endl;
ofHtmlFile << "<TITLE>" << endl;
ofHtmlFile << "Kenny's array table" << endl;
ofHtmlFile << "</TITLE>" << endl;
ofHtmlFile << "Welcome to this computer generated webpage! <br>" << endl;
ofHtmlFile << "</HEAD>" << endl;
return;
}
void writeFooter( ofstream& ofHtmlFile)
{
ofHtmlFile << "</HTML>" << endl;
return;
}
void writeBody (ofstream& ofHtmlFile, int nArray[], int nArraySize)
{
ofHtmlFile << "<BODY>" << endl;
ofHtmlFile << "This is where you place your content." << endl;
ofHtmlFile << "<TABLE BORDER=\"1\">" << endl;
ofHtmlFile << "<th> KEY </th>" << endl;
ofHtmlFile << "<th> VALUE </th>" << endl;
for (int i=0; i<=10; i++)
{
ofHtmlFile << "<tr><td>" << i << "</td><td>" << nArray[i] << "</td></tr>"; //this seems to be the line with the problems.
}
ofHtmlFile << " </TABLE>" << endl;
ofHtmlFile << "</BODY>" << endl;
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment