Hi, i am still having problems. Still not 100% sure what i have to do.
Would it be possible to take a look at my code and point me in the right direction?
CODE
// This programme reads a file, works out the number of Characters, Lines, Tags and comments and prints the results on the screen.
// It will also try to create a new file and copy the contents of the existing file into the new file.
// Then it will try to convert the HTML tags to lowercase.
#include <fstream>
#include <string>
#include <cstdlib>
#include <iostream>
using namespace std;
int numCharsInFile( ifstream &in, int &numLines ); // This command should count the number of Chatacters and line in the file
int numTagsInFile ( ifstream &in, int &numComents); // This command should count the number of Tags and Comments in the file.
string getInputFileName();
string getOutputFileName();
int main ()
{
char c;
int
noChars,
noLines,
noTags,
noComments,
notemp;
bool badfilename;
ifstream inputFile;
ofstream outputFile;
string fileName;
string newFile;
// ********************************************************************************
*************************************************
do
{
fileName = getInputFileName();
newFile = getOutputFileName();
badfilename = (fileName == newFile);
if (badfilename)
{
cout << "Bad File name, Please choose a different name." << endl;
}
}
while (badfilename);
// ********************************************************************************
*************************************************
// Try to open the specified file.
inputFile.open(fileName.c_str());
if( !inputFile.is_open() )
{
cerr << "ERROR! Cannot open the the following file...: " << fileName << endl << endl;
exit (0);
}
outputFile.open(newFile. c_str());
if (!outputFile)
{
cout << "Sorry, an Error occurred whilst inputting your file." <<newFile <<"\n";
return 1;
}
// ********************************************************************************
***************************************************
// Print the correct outputs on screen. Characters, Lines, Tags and Comments.
noChars = numCharsInFile( inputFile, noLines ); // To determine the number of lines and characters in the .html file
noTags = numTagsInFile ( inputFile, noComments ); // To count the tags and comments in the .html file.
cout << "There are: " << noChars << " Characters in the file " << fileName << endl << endl;
cout << "There are: " << noLines << " Lines in the file " << fileName << endl << endl;
cout << "There are: " << noComments << " Comments in the file " << fileName << endl << endl;
cout << "There are: " << noTags << " Tags in the file " << fileName << endl << endl;
inputFile.close();
return 0;
}
// ********************************************************************************
****************************************************
// Ask the programme user for the input file name.
string getInputFileName()
{
string currentfileName;
cout << "Please enter the file name including the file path: " << endl;
cin >> currentfileName;
cout << endl;
return currentfileName;
}
// Ask the programme user for the output file name.
string getOutputFileName()
{
string outputName;
cout << "Please enter a name for the file you wish to create. \nIncluding the Path you where you want it to be created:" <<endl;
cin >>outputName;
cout << endl;
return outputName;
}
// ********************************************************************************
******************************************************
int numCharsInFile( ifstream &in, int &numberLines )
{
int numberChars = 0;
char cholder; // This is my character holder;
numberLines = 0; // Re-set the number of lines to zero, this is important for accuracy reasons.
while (in.get(cholder))
{
if (cholder != ' ')
{
if(cholder != '\n')
{
numberChars++; // If character holder is = to \n and not a blank space then increase the character count by one.
}
else
{
numberLines++; // Increase the number of lines by one if \n
}
}
}
numberLines += 1;
return numberChars;
}
// ********************************************************************************
****************************************************
int numTagsInFile ( ifstream &in, int &numberComments)
{
in.clear();
in.seekg(0, ios_base::beg);
int numberTags =0;
numberComments =0;
char cholder;
while (in.get(cholder))
{
if ( cholder == '<')
{
if( cholder != '!')
{
numberTags++;
}
}
else if (cholder == '!')
{
numberComments++;
}
}
return numberTags;
}
// ********************************************************************************
*****************************************************
I have tried adding something in along these lines...
CODE
{
char str[2000];
fstream file_op("fileName",ios::in);
while(!file_op.eof())
{
file_op.getline(str,2000);
cout <<str;
} file_op.close();
cout <<endl;
return 0;
}
But got nowhere :-(
Thanks.