i am in a c++ class and we have a problem that consists of retrieving information from a data file and looking through the file and pulling out all of the strings with the @ symbol in it. the catch is some of the email addresses have a comma afterwards and i have no clue how to take the comma off the end of an unknown length string. here is what my code looks like now
#include <fstream>
#include <string>
using namespace std;
int main()
{
string phrase;
ifstream mail;
ofstream addresses;
// remove_comma.cpp : main project file.
#include "stdafx.h" // Visual Studio Specific, remove for other compilers
#include <iostream>
#include <string>
#include <fstream>
usingnamespace std;
int main(array<System::String ^> ^args) // Visual Studio Specific
// int main() // use this if no parameters from os are passed
// int main(int argc, char* argv[]) // or this if parameters are passed to main
{
string phrase;
fstream mail; // personally I prefer using fstream instead of ifstream and ofstream
fstream adress;
// open adress.dat for input at the begin of the file.
adress.open("adress.dat", ios::in | ios::beg );
// open mail.dat truncate (create new/overwrite) for output.
mail.open("mail.dat", ios::out | ios::trunc);
// this adres.eof() returns true if End Of File has been reached
while(!adress.eof())
{
// get one line from adress and put it into phrase.
getline(adress,phrase);
if(phrase.find("@")!=string::npos) // true if an @ sign is found.
{
if(phrase.find(",")!=string::npos) // true if a comma has been found.
{
// remove everything from the position of the comma to the end of string phrase.
// phase.find() returns a size_t value for the location of the comma.
// this return value is a parameter for erase after find() has been executed.
phrase.erase(phrase.find(","));
cout<<"comma removed from: "<<phrase<<" and saved.\n";
}
// all else statements and cout calls can be removed, its just to make things visual.
else
{
cout<<"clean emailadres: "<<phrase<<" saved\n";
}
// put the cleaned up phrase string into "mail.dat"
mail<<phrase<<"\n";
}
else
{
cout<<"not an emailadres, disposed: "<<endl<<phrase<<endl;
}
// clear the contents of phrase, I'm not sure if this is nessecary.
phrase.clear();
}
// close both files.
adress.close();
mail.close();
return 0;
}
I used the sample file adress.dat for testing:
1 2 3 4
not valid email
info@rvasolutions.nl
obama@whitehouse.gov,
someone@someserver.com,