GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 05-Dec-2007, 16:27
BoomStick BoomStick is offline
New Member
 
Join Date: Dec 2007
Posts: 8
BoomStick is on a distinguished road

Text erase?


Iam writing a program that takes numbers and saves them to a text file (numbers.txt) than i would like it to take the numbers from numbers.txt and erase any odd numbers and save it to evenNumbers.txt, so in the end i would have two files, so far i am able to create and save the numbers in numbers.txt but iam not sure how to erase the odd numbers, any help would be much appreciated

CPP / C++ / C Code:
#include <iostream>
# include <fstream>
using namespace std;


int main ()
{
	int number = 0;

	ofstream outFile;
	outFile.open("numbers.txt", ios::out);

	if (outFile.is_open())
	{
		cout << "enter number to be added to 'numbers.txt', or a letter to exit" << endl<< "**note ODD numbers will be excluded**" << endl;
		cin >> number;
		//int.erase('1'); //////was using this to test getting rid of '1'
		outFile << number << endl;
		
	}
	outFile.close();
 
}
 

thanks in advance

-boomstick
  #2  
Old 05-Dec-2007, 18:17
Kimmo Kimmo is offline
Regular Member
 
Join Date: Mar 2007
Location: Finland
Posts: 450
Kimmo is a jewel in the roughKimmo is a jewel in the roughKimmo is a jewel in the rough

Re: text erase?


Well, if you know how to save all the numbers, all you need to do is read the numbers and only write the odd numbers to the new file. No need to delete anything.
  #3  
Old 06-Dec-2007, 01:43
inevitable inevitable is offline
Junior Member
 
Join Date: Nov 2007
Posts: 53
inevitable is on a distinguished road

Re: text erase?


For deleting the content, you can use '\b' or ascii value of "backspace" key stroke.. That would replace the number(Character i.e 13 will become 1) with backspace character.....


Hope you understood what to do. If you are still unable then, I will give you the code. But I recommend you do it by yourself.
  #4  
Old 06-Dec-2007, 12:04
BoomStick BoomStick is offline
New Member
 
Join Date: Dec 2007
Posts: 8
BoomStick is on a distinguished road

Re: text erase?


iam not sure what iam missing than, iam sure its something real simple, ive been messing around with a few different things but still am getting no where
  #5  
Old 06-Dec-2007, 12:12
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 969
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: text erase?


Why don't you post your code again and we'll take a look.
  #6  
Old 06-Dec-2007, 13:02
BoomStick BoomStick is offline
New Member
 
Join Date: Dec 2007
Posts: 8
BoomStick is on a distinguished road

Re: text erase?


Quote:
Originally Posted by fakepoo
Why don't you post your code again and we'll take a look.
i screwed something up and reverted back to the original code posted...
  #7  
Old 06-Dec-2007, 13:14
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 969
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: text erase?


I think you should write a function that reads from one stream and writes to another.

CPP / C++ / C Code:
// Filters out all number except even ones
void FilterEven( ifstream& input, ofstream& output )
{ int i;
  while( /* input has not reached the end of file */ )
  { // read an integer

    // if it is even, write it to output
  }
}

int main()
{ ifstream input("input.txt");
  ofstream output("output.txt");
  
  FilterEven(input,output);
  
  input.close();
  output.close();
  return 0;
}
  #8  
Old 06-Dec-2007, 14:03
BoomStick BoomStick is offline
New Member
 
Join Date: Dec 2007
Posts: 8
BoomStick is on a distinguished road

Re: text erase?


Quote:
Originally Posted by fakepoo
I think you should write a function that reads from one stream and writes to another.

CPP / C++ / C Code:
// Filters out all number except even ones
void FilterEven( ifstream& input, ofstream& output )
{ int i;
  while( /* input has not reached the end of file */ )
  { // read an integer

    // if it is even, write it to output
  }
}

int main()
{ ifstream input("input.txt");
  ofstream output("output.txt");
  
  FilterEven(input,output);
  
  input.close();
  output.close();
  return 0;
}


Well I was wanting to have it so it would create the numbers.txt than take the info from that and filter out the evens, I suppose thats kinda like what you are saying isn't it?
  #9  
Old 06-Dec-2007, 14:13
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 969
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: text erase?


I thought you already got file creation part working??? The sample I posted is for taking the file that already has numbers in it and creating a new file with all of those numbers except for the odds.
  #10  
Old 06-Dec-2007, 19:49
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,432
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: text erase?


Quote:
Originally Posted by BoomStick
Iam writing a program that takes numbers and saves them to a text file (numbers.txt) than i would like it to take the numbers from numbers.txt and erase any odd numbers and save it to evenNumbers.txt, so in the end i would have two files, so far i am able to create and save the numbers in numbers.txt but iam not sure how to erase the odd numbers, any help would be much appreciated
Obviously you are writing two programs.

The first one is simple. Based on the code you posted, read a number from the keyboard and write it to the file. Your first problem is that when reading numbers you cannot accept a letter to exit. You'll need some other way to exit the program.

Second program: read the first file and output only the even numbers to the new file. Do you know how to test if a number is odd or even?
__________________

Definition: Politics
Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers
 
 

Recent GIDBlogGID Spam Detector 1.1.0 by gidnetwork

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
burning problems PLEASE PLEASE HELP kelticeire Computer Hardware Forum 4 01-Dec-2006 15:39
need help on text file analyser ffantasy C Programming Language 1 05-Sep-2005 13:44
saving html text dopee MySQL / PHP Forum 1 17-Jan-2005 04:15
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 10:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 11:28

Network Sites: GIDNetwork · GIDApp · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:19.


vBulletin, Copyright © 2000 - 2013, Jelsoft Enterprises Ltd.