School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
Welcome to Dream.In.Code
Become an Expert!

Join 340,049 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 4,528 people online right now. Registration is fast and FREE... Join Now!



File IO Copy contents and save to new file

File IO Copy contents and save to new file Rate Topic: -----

#1 rotoro  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: New Members
  • Posts: 4
  • Joined: 03-April 09


Dream Kudos: 0

Post icon  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.
Was This Post Helpful? 0
  • +
  • -


#2 no2pencil  Icon User is offline

  • PHoToN PoWeR PaCK : not included
  • Icon
  • View blog
  • Group: Moderators
  • Posts: 15,043
  • Joined: 10-May 07


Dream Kudos: 2875

Expert In: Goofing Off

Posted 03 April 2009 - 01:58 AM

View Postrotoro, 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. :D :^:
Was This Post Helpful? 0
  • +
  • -

#3 rotoro  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: New Members
  • Posts: 4
  • Joined: 03-April 09


Dream Kudos: 0

Posted 03 April 2009 - 02:05 AM

View Postno2pencil, on 3 Apr, 2009 - 01:58 AM, said:

View Postrotoro, 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. :D :^:


Thanks, i will give that a go!! I will no doubt be back in a hurry ha! Cheers.
Was This Post Helpful? 0
  • +
  • -

#4 rotoro  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: New Members
  • Posts: 4
  • Joined: 03-April 09


Dream Kudos: 0

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?

 // 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.
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is online

  • Mayor of Simpleton
  • Icon
  • View blog
  • Group: Moderators
  • Posts: 7,122
  • Joined: 23-August 08


Dream Kudos: 50

Expert In: Being annoyed with lazy people.

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:

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

Was This Post Helpful? 0
  • +
  • -

#6 rotoro  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: New Members
  • Posts: 4
  • Joined: 03-April 09


Dream Kudos: 0

Posted 03 April 2009 - 04:20 AM

By got nowhere, i mean i dont really understand what i am doing. I tried adding it in and my programme just stopped. Just keep sending myself round in circles. Never dealt with FIle IO before.
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is online

  • Mayor of Simpleton
  • Icon
  • View blog
  • Group: Moderators
  • Posts: 7,122
  • Joined: 23-August 08


Dream Kudos: 50

Expert In: Being annoyed with lazy people.

Posted 03 April 2009 - 06:29 AM

Well, I just gave you pseudocode. Here and here are tutorials on C++ File I/O. Have at it.
Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month