1 Replies - 683 Views - Last Post: 23 February 2009 - 10:32 AM Rate Topic: -----

Topic Sponsor:

#1 gmmechanic2003  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-February 09

Reading input files which writes only numeric data to a new file

Post icon  Posted 23 February 2009 - 09:57 AM

I am unable to take an existing file and write a new file that just displays the numeric data in the new file. I don't know what command is required to do this task

Secondly, in the same program I need to write another new file that take the same file as above and writes just the alphabetic data to a new file, with both keeping the same format (spacing/white space)

And lastly I cannot figure out the else and if for this program to write the second file. Any help would be greatly appreciated.


#include <fstream>
#include <iostream>
#include <cctype>
using namespace std;

int main ()
{
	char ch;
	ifstream fin;
	ofstream fout;
	//string filename;



if(fin.fail())
{
	cout<<"could not open input file"<< endl;
	exit(1);
}

else
{
fin.open("uppernumeric.txt");
fout.open("numeric.txt");
fin.get(ch);
}
if (fin.fail())
{
fin.open("uppernumeric.txt");
fout.open("alphabetic.txt");
fin.get(ch);
while(!fin.eof());
}
{

ch=isalpha(ch);

cout<<ch;
fout.put(ch);
fin.get(ch);
while(!fin.eof())
{

ch=isdigit(ch);

cout<<ch;
fout.put(ch);
fin.get(ch);
}

fin.close();
fout.close();
}

return 0;
}




Is This A Good Question/Topic? 0
  • +

Replies To: Reading input files which writes only numeric data to a new file

#2 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 288
  • View blog
  • Posts: 1,898
  • Joined: 25-October 06

Re: Reading input files which writes only numeric data to a new file

Posted 23 February 2009 - 10:32 AM

the function ialpha() returns non-zero (true) if the character is alphabetic, see
http://www.cpprefere.../string/isalpha

you would use it so to write out alphabetic only
if(isalpha(ch))
  {
  cout<<ch;
  fout.put(ch);
  }


you can use isdigit() is a similar way to test for digits
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1