Hello, I had a question about a CS program. We were asked to write a program for a file that reads mail.dat and output all string containing the "@" sign to file adresses.dat. We are supposed to use strings for this..

My thoughts are that we should go invididually, character by character to see if the word we are currently at contains "@" if it does, we want to output the whole string to address.dat. Obviously this programs main purpose is to record an email address.

For example for the following mail:

From: [email]someone@marripan.edu[/email]
Date: Wed, 13 Aug 2003 17:12:33 EDT
Subject: stocks
To: [email]john@meringue.com[/email]

The program would then output the two email addresses to addresses.dat.
How would i write the program to go character by character and extract the email addresses with the @ sign

Try to break the program into smaller, more manageable segments. I'd have an ifstream that opens the file and reads from the file one string at a time, and an ofstream for the output of e-mail addresses. For each string read in, decide whether the '@' character is present. If not, read in the next string from the ifstream. If '@' IS present in the string, send that entire string to the ofstream, which puts that e-mail address in your output file. As for deciding whether a particular string has a '@' in it, you can go character by character through that string and set a boolean flag if and when you come across '@', or the "find" function from the string library could come in handy. Don't read from the file one character at a time. Read from the file one string at a time, then test that string for the '@' character and act accordingly based on whether '@' is present in the string.

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

Your going to need a function that looks something like this.

//while there is a word in the file, put it into a string that you have 
//declared, inword.*/

  while(infile >> inword)
    {

//examine each character of the string one by one using inword.length(), 
//a built in string function.

      for(int i = 0; i < inword.length(); i++)
        {                                                  

//if a character is '@', output the whole string into a file

          if(inword[i] == '@')
            {
              outfile << inword << endl;
            }
        }

    }

As hockeyplayer suggests, read a line at a time. Then use .find() to locate the @ if present. If you find it, set up 2 loops:
Loop 1 tests each character before the @ to find the first character that is illegal in an email address (such as the ']')
Loop 2 does the same for each character after the @

you could also use basic_string<>::find_first_not_of and basic_string::find_last_not_of to locate the characters at either end that are illegal in an email address. and the use basic_string<>::substr to extract the email address.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.