LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-16-2008, 03:02 PM   #1
rec3g
LQ Newbie
 
Registered: Mar 2008
Posts: 12

Rep: Reputation: 0
I'm having trouble with this program....


so I'm trying to write this program that reads in email addresses from a file called mail.dat and outputs every string containing the @ sign to a file called addresses.dat. I'm not very good and these kinds of program (or any for that matter! ) but I'm trying..and so far not having much luck. Right now, I'm getting this error...
Quote:
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../lib/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status
my program looks like this...
Quote:

#include <string)
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
ifstream mail;
ofstream addr;
string chunk;
mail.open("mail.dat");
addr.open("addresses.dat");

mail >> chunk;
while (mail)
{
if mail.find('@')
mail >> chunk;
}
chunk >> addr;

}

I honestly don't even really understand all of this... I know ifstream is the input file and ofstream is the output file but I'm not sure if I set the while statement up right ..or any of the last part. I would greatly appreciate any pointers anyone could give me!
 
Old 03-16-2008, 03:16 PM   #2
DragonSlayer48DX
Registered User
 
Registered: Dec 2006
Posts: 1,454
Blog Entries: 1

Rep: Reputation: 74
Hi, and welcome to LQ!

You might want to click the Report button on the bottom of your post and ask a mod to move this thread to the Programming forum so your question can get the exposure and attention it deserves. Also, you might find the Programming link in my sig useful.

Cheers
 
Old 03-16-2008, 03:24 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911
Are you sure that the code you posted gets that far in the compilation process
as to start linking? I can see two flaws right away that should make g++ spit
the dummy way further up the line...

E.g. the closing ) instead of > on the first line.



Cheers,
Tink


P.S.: Moving this to programming.
 
Old 03-16-2008, 03:25 PM   #4
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
How exactly are you compiling?

Also, you can use "while(mail >> chunk)" to keep reading from mail until there's nothing left.
 
Old 03-16-2008, 03:29 PM   #5
rec3g
LQ Newbie
 
Registered: Mar 2008
Posts: 12

Original Poster
Rep: Reputation: 0
I typed aCC "filename" -o "executable" to compile
 
Old 03-16-2008, 03:47 PM   #6
rec3g
LQ Newbie
 
Registered: Mar 2008
Posts: 12

Original Poster
Rep: Reputation: 0
oh ..and fyi..I fixed the ')' error on the first line...

program still doesn't compile
 
Old 03-16-2008, 03:53 PM   #7
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 76
Quote:
Originally Posted by rec3g View Post
I typed aCC "filename" -o "executable" to compile
This is a C++ program and you have to use a C++ compiler (e.g., g++). The correct way is:
Code:
g++ -o output source.cc
Edit: disregard this, I thought aCC was a C compiler, not a C++ compiler.

Here is what I think you want your source to be:
Code:
#include <string>
#include <iostream>
#include <fstream>

using std::ifstream;
using std::ofstream;
using std::string;

int main() {
	ifstream mail;
	ofstream addr;
	string chunk;

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

	while (mail >> chunk) {
		if(chunk.find('@') != string::npos)
			addr << chunk << std::endl;
	}
}

Last edited by osor; 03-16-2008 at 03:59 PM.
 
Old 03-16-2008, 04:08 PM   #8
rec3g
LQ Newbie
 
Registered: Mar 2008
Posts: 12

Original Poster
Rep: Reputation: 0
yeah that's perfect! I had been working on it for a little while and I almost had the exact same thing..except I had "mail" instead of "chunk" in the if statement for find. One last question...how would you remove any commas that might be on the end of the email addresses? Wouldn't you use subtr in some way?
 
Old 03-16-2008, 04:09 PM   #9
rec3g
LQ Newbie
 
Registered: Mar 2008
Posts: 12

Original Poster
Rep: Reputation: 0
correction...it should be substr instead of subtr..whoops
 
Old 03-16-2008, 05:01 PM   #10
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 76
Quote:
Originally Posted by rec3g View Post
One last question...how would you remove any commas that might be on the end of the email addresses? Wouldn't you use subtr in some way?
Are you sure a more text-oriented language (e.g., Perl) might not better suit your needs?

If not, what kind of input/output are you envisioning? If you want input that looks like the following:
Code:
$ cat mail.dat
someone@somewhere.com,someone@somewhereelse.com,not an address,enoemos@erehwemos
Then you might do this:
Code:
#include <string>
#include <iostream>
#include <fstream>

using std::ifstream;
using std::ofstream;
using std::string;

int main() {
	ifstream mail;
	ofstream addr;
	string chunk;

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

	while (getline(mail, chunk, ',')) {
		if(chunk.find('@') != string::npos)
			addr << chunk << std::endl;
	}
}
If you are envisioning input data like this:
Code:
$ cat mail.dat
someone@somewhere.com,
someone@somewhereelse.com,
not an address,
enoemos@erehwemos
you might do the following:
Code:
#include <string>
#include <iostream>
#include <fstream>

using std::ifstream;
using std::ofstream;
using std::string;

int main() {
	ifstream mail;
	ofstream addr;
	string chunk;
	size_t last;

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

	while (mail >> chunk) {
		if(chunk.find('@') != string::npos) {
			last = chunk.length() - 1;
			if(chunk[last] = ',')
				chunk[last] = '\0';
			addr << chunk << std::endl;
		}
	}
}
If you are envisioning data like this:
Code:
$ cat mail.dat
someone@somewhere.com,someone@somewhereelse.com
not an address,enoemos@erehwemos
You will have to tokenize each line of input. The easiest way to do this is through something like boost::tokenizer or boost::algorithms::split instead of doing it yourself. You might also revert to the C-style strtok_r() function which should be available to your C++ code.
 
Old 03-16-2008, 07:32 PM   #11
rec3g
LQ Newbie
 
Registered: Mar 2008
Posts: 12

Original Poster
Rep: Reputation: 0
uhh wow....I'm a beginner at C++ and half of what you were saying is completely over my head. It sounds great though! haha ..if I know what you meant. I'm taking a class on C++ and we can only use what we've discussed so far in class. So, therefore I can't really use any of what you suggested(mainly because I don't know how!) Thanks for the suggestions though
 
Old 03-16-2008, 07:54 PM   #12
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911
I think what osor is getting at was that you're going to get ANY line
from the file that contains an @ ... whether that's part of a valid
e-Mail address or just some randomly thrown in character to express
swearing ;} .... it will also add the whole line if an e-Mail address
appears anywhere in the text to your addresses.dat file, rather than
just the address.



Cheers,
Tink
 
Old 03-16-2008, 08:41 PM   #13
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 76
Quote:
Originally Posted by rec3g View Post
I'm a beginner at C++ and half of what you were saying is completely over my head.
How about the other half? What does mail.dat look like, and what kind of output do you want in addresses.dat?
 
Old 03-16-2008, 08:54 PM   #14
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
regex.h might be an option. That's really the best way to capture all valid email addresses and nothing else. Next the question would be "what is a valid email address?" I've actually seen email addresses (though only used on an isolated WAN) containing "/". I don't know if that's actually valid, or in fact what characters are allowed as the username portion (as far as the protocol.)
ta0kira
 
Old 03-16-2008, 09:23 PM   #15
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911
The RFC is 2822 ... and below a page that builds a regex to extract valid
addresses ... I didn't verify its correctness yet, though.

http://www.regular-expressions.info/email.html


;}



Cheers,
Tink
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
ASM Program Trouble Mercurius Programming 6 11-08-2006 09:18 PM
I am having trouble installing a program chesterthefatp Linux - Newbie 2 01-10-2006 03:57 PM
having trouble with the do while loop in program mshinska Programming 2 10-08-2005 10:18 PM
Having trouble using libwnck in my program. rob_of_ownsboro Programming 1 05-03-2005 04:25 PM
trouble configuring a program minm Linux - Newbie 12 03-28-2005 04:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration