i/o stream
ok, im having a prop, the way im doing it must not be right, im trying to compare a char address[256], which convert to string addr, i want to compare it to a list of addresses in a text file, my prop is it will not read any of the address, whats the best way for this?
[270 byte] By [
mucker22] at [2007-11-19 19:30:46]

# 1 Re: i/o stream
So you have some code that is not working? Why don't you show it to us?
cilu at 2007-11-10 23:43:37 >

# 2 Re: i/o stream
boolean urlFilter(char addr[256] )
{
int position=0;
string line2;
string line;
line = addr;
int length;
cout<<line<<"kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk"<<endl;
ifstream myfile ("url.txt",ios::in);
while(! myfile.eof())
{
getline(myfile,line2); position = line2.find(line);
cout<<position<<" = the position"<<endl;
if(position>0)
{
cout<<" true returned so "<<endl;
return true;
break;
}
else
{
cout<<" false returned so "<<endl;
return false;
}
cout<<" position put back to 0 "<<endl;
position=0;
}
myfile.close();
}
# 3 Re: i/o stream
It's a little hard to follow because of the indentation (use the code-tag
feature), but it looks like if the first line does not match, you return
false, instead of reading the rest of the file.
Also, your check is incorrect ... change as follows:
string::size_type position=0; // not int
if(position != string::npos)
{
cout<<" true returned so "<<endl;
return true;
}
# 4 Re: i/o stream
but i have while(! myfile.eof()) so i should go til the end of the file?
# 5 Re: i/o stream
yuo do the following steps within the while loop (simplified):
1) read a line
2)
if (position > 0)
return true;
else
return false;
so it never loops.
3) you can use the debugger (or put a cout after the getline()
to verify this).
# 6 Re: i/o stream
im sorry, im still a bit confused, would you be able to explain the whole thing what i have to do again
thanks
# 7 Re: i/o stream
In your current code, does the text :
" position put back to 0 "
every get output ? It doesn't, because you return before that
even if the line does not match.
You should not have a "return false" until you leave the loop.
Something like this:
bool urlFilter(char addr[256] )
{
string::size_type position=0;
string line2;
string line (addr);
ifstream myfile ("url.txt",ios::in);
while(getline(myfile,line2))
{
position = line2.find(line);
if(position != string::npos)
{
return true;
}
}
return false;
}
# 8 Re: i/o stream
when i run it it is not reading anything from the text file, it goes itno the method alright but it does, but it doesnt enter the while loop