473,320 Members | 1,979 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Help finding a string in a file

3
I'm having a little trouble finding multiple occurrences of a string in a file. I have a txt file and I have to find e-mail addresses out of it. I open the file with ifstream, and use getline to read in each line. I then use str.find(str1, pos) to find the '@' symbol in the file. I just can't figure out how to pull the entire address out and not just the @ symbol. Any tips?
Nov 13 '07 #1
5 2302
Studlyami
464 Expert 256MB
is there a reason you are going line by line? if not you can just step through the file via the spaces ifile>>MyString; then check the string to see if there is a @ symbol in it. If so, MyString = e-mail address (assuming all items that have @ is an e-mail).

You can also take the location returned from the find function and keep on stepping back until you reach a ' ' or the string[0]. Then you would have to step forward until you hit a ' ' or a '\n'.
Nov 13 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
I would use an iterator and step through until I found the @. This is one past the address so this works as an end iterator with the copy algorithm. So, you just copy from begin() to your iterator pointing at the @.
Nov 14 '07 #3
sg14
3
Well, I changed my algorithm. I'm now searching for "mailto:". This is what I have (it doesn't work).

string sub;
int found_position = 0;

//string s = " ";
char temp_s[200];
string found_email = " ";
char temp_sub[50];

while (! instream.eof() )
{

instream.getline(temp_s, 200); //reads in string of 200

str1 = temp_s; //use the constructor of string to build a string
sub = temp_sub;

found_position = str1.find("mailto:"); //tell me where "mailto:" is

sub = str1.substr(found_position + 7); //string after mailto:



if(found_position >= 0)
{
for (unsigned int index = 0; index < sub.length(); index ++)
{
if(sub[index] == '"') //checks for spaces (end of address)
{
break;
}
found_email += sub[index];
}
cout << found_email << endl;
emailList.push_back(found_email);
}
}


It doesn't work :-( Any tips?
Nov 21 '07 #4
sg14
3
It's for a homework assignment, so I don't know anything about iterators or anything, just basic c++ stuff. :-(
Nov 21 '07 #5
Studlyami
464 Expert 256MB
First use the code tags provided. When you hit reply there are reply guidelines to the right which provides the description of how to use them. Second could we see what your input file looks like (just a few lines will do), or how the assignment describes what the input file should be like. Third "It doesn't work" isn't a very good description of the problem. Does it not compile? Does the program crash? Does your vector contain 0 items? Here is a couple of items i see.

In the input file is it mailto:<EmailAddyHere> or mailto: <EmailAddyHere> (notice the space).
Expand|Select|Wrap|Line Numbers
  1. if(sub[index] == "") // does that even compile?  you are checking for a char so
  2. if(sub[index] == ' '); use single quotes when checking chars
  3.  
if you have mailto: <emailaddy> you will exit the loop without copying the string items because you have a space between mailto: and the e-mail address.
Nov 21 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Brad Isaacs | last post by:
I am working with ASP.NET 2.0 and using an SQL Server 2000 database. I am using Visual Studio 2005 and developing on my Local machine. I am working with Login controls ASP.Configuration, I...
3
by: Simon Brooke | last post by:
As various people will have noticed, I've been having a lot of trouble with XSL lately. Brief history: I wrote myself an XML toolkit back in 2000, and it worked well enough for me, so it's been...
1
by: =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= | last post by:
I get the above error in some of the ASP.NET web applications on a server, and I need some help figuring out how to deal with it. This is a rather long post, and I hope I have enough details that...
1
by: Someone21 | last post by:
Hi all, I am currently trying to retrieve a string from a textfile, separate that and put it in an array. I stumbled on the problem that *token is a pointer, and not the string. However when i...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.