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 11-May-2008, 23:38
deejman deejman is offline
New Member
 
Join Date: May 2008
Posts: 3
deejman is on a distinguished road
Question

Need some direction, trying to extract email addresses from text file


Hi everyone,

My name is DJ and I'm a really green newbie. I'm working on an assignment for my class, learning C++. The project is to extract valid email addresses from any text file, like an HTML source page. There may be more than 1 email per line, but I haven't gotten to that part yet. I am not versed with vector or find , as I have found a few programs to do this that include vector and find, but I'm in programming 101 and we haven't learned those yet.

Here's my code so far. I'm sure my for loops are causing some of the problem, I just can't seem to put my finger on what. I'm completely stuck. I've added extra cout lines to try and figure out what's going on, but I'm still lost.

Any help would be greatly appreciated.

Thanks,
Deej

--------------------------------------------------------------------
CPP / C++ / C Code:
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
 
bool validChar (int n)
{
  bool result = false;
  if ((n > 47 && n < 58 ) || (n > 64 && n < 91)|| (n > 96 &&  n < 123)
    || (n == 43) || (n == 45) || (n == 46) ||(n == 95)) result = true;
  return result;
}
 
int main()
{
  string defInFile = "fileContainingEmails.txt";
  string defOutFile ="copyPasteMyEmails.txt";
  string inFile;
  string outFile;
 
  cout << "\n Enter the input file name, leave blank for fileContainingEmails.txt: ";
  getline(cin, inFile);
 
  if (inFile.length()==0)
  {
    inFile=defInFile;
    outFile=defOutFile;
  }
  else outFile=inFile;
 
cout << "inFile is " << inFile << endl;
cout << "outFile is " << outFile << endl << endl;
 
  string temp;
  cout << "\n Enter the output file name, leave blank for " << outFile << ": ";
  getline(cin, temp);
 
  if (temp.length()==0) outFile=outFile;
  else outFile=temp;
 
cout << "\n inFile is " << inFile << endl;
cout << " outFile is " << outFile << endl << endl;
 
 
  ifstream fin;
  ofstream fout;
 
  fin.open(inFile.c_str());
  if (!fin.good()) throw "I/O error";
 
  int s, e;
  string anEmail;
  while (true)
  {
    if (!fin.good()) break;
 
    string line;
    getline(fin, line);
 
    for (int i = 0; i < line.length(); i++)  // as long as i is < entire length of line, increment by 1
    {
      if (line[i]==64) // if @ symbol present, print line
      {
        for (s = i; s > 0; s--)
        {
          if (!validChar(line[i])) break;
        } // for
        s++;
cout << "         S IS    " << s << endl;
 
        for (e = i; e > line.length(); e++)
        {
          if (!validChar(line[i])) break;
          if (line[i] == line.length()) break;
        }  // for
 
        anEmail = line.substr(s, e-s);
cout  << "      anEmail     : " << anEmail << endl;
        }  // if
    }  // for
  }  // while
  fin.close();
  return 0;
}
Last edited by admin II : 12-May-2008 at 19:45. Reason: Changed [CODE] to [CPP]
 
 

Recent GIDBlogToyota - 2008 August Promotion by Nihal

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
Bulk Email hosting *** .com Bullet proof hosting additional2 Web Hosting Advertisements & Offers 1 20-May-2007 13:55
bulk Email Marketing Bullet proof hosting additional2 Web Hosting Advertisements & Offers 0 08-Jul-2006 12:57
After execution - Error cannot locate /Skin File? WSCH C++ Forum 1 05-Mar-2005 20:03
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 · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:37.


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