removing characters off of an unknown length string

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;

mail.open("mail.dat");
addresses.open("address.dat");

mail >> phrase;
while (mail)
{
if(phrase.find("@")!=string::npos)
{
addresses << phrase << endl;
}
mail >> phrase;
}

mail.close();
addresses.close();

return 0;
}
Look at string's substr() method and/or erase() method.
ok i will try that. thanks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// remove_comma.cpp : main project file.
    
#include "stdafx.h"  // Visual Studio Specific, remove for other compilers
#include <iostream>
#include <string>
#include <fstream>

using namespace 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,


please refere to

http://www.cplusplus.com/reference/iostream/fstream/

and to

http://www.cplusplus.com/reference/string/string/

for more information on members of fstream and string.

I hope this solves your problem.

Regards, Ronnie van Aarle.
thanks Ronnie. that helped out a lot. i finished my project now its time to turn it in. fingers crossed!
Topic archived. No new replies allowed.