File IO Copy contents and save to new file
Page 1 of 1
File IO Copy contents and save to new file
#1
Posted 03 April 2009 - 01:55 AM
Hi i was wondering if somebody could help me please.
I have an assignment where i have to convert html tags in a file to lowercase. But before i do that i have to copy the contents on one file. For example "html.htm" into a new file "html2.htm.
I have no problem with my programme creating a new file "html2" when prompted, but when it creates the file it is blank. Could anyone please give me some advice on how to copy the contents of html.htm into html2.htm?
Thanks in advance.
Lee.
I have an assignment where i have to convert html tags in a file to lowercase. But before i do that i have to copy the contents on one file. For example "html.htm" into a new file "html2.htm.
I have no problem with my programme creating a new file "html2" when prompted, but when it creates the file it is blank. Could anyone please give me some advice on how to copy the contents of html.htm into html2.htm?
Thanks in advance.
Lee.
#2
Posted 03 April 2009 - 01:58 AM
rotoro, on 3 Apr, 2009 - 03:55 AM, said:
Could anyone please give me some advice on how to copy the contents of html.htm into html2.htm?
Open two file pointers, one for read & one for write.
Read in one character at a time, & check it for EOF. While that character is not EOF, convert it as requested, & then write it to the output file.
#3
Posted 03 April 2009 - 02:05 AM
no2pencil, on 3 Apr, 2009 - 01:58 AM, said:
rotoro, on 3 Apr, 2009 - 03:55 AM, said:
Could anyone please give me some advice on how to copy the contents of html.htm into html2.htm?
Open two file pointers, one for read & one for write.
Read in one character at a time, & check it for EOF. While that character is not EOF, convert it as requested, & then write it to the output file.
Thanks, i will give that a go!! I will no doubt be back in a hurry ha! Cheers.
#4
Posted 03 April 2009 - 04:00 AM
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?
I have tried adding something in along these lines...
But got nowhere :-(
Thanks.
Would it be possible to take a look at my code and point me in the right direction?
// 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...
{
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.
#5
Posted 03 April 2009 - 04:06 AM
What is the problem exactly? "Got nowhere" is just not helpful. In your final bit of code this is what you're doing:
Do you see a problem there?
Better:
open file while file pointer is not at end of file start read a line from file output line close file end
Do you see a problem there?
Better:
open file for reading if file was successfully opened start while we can read line from file start output line end close file end
Page 1 of 1

Start a new topic
Add Reply




MultiQuote


| 


