By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,997 Members | 1,470 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,997 IT Pros & Developers. It's quick & easy.

read file

P: n/a
Hello,
I have a text file and I have to check if the line I read contains some
word,the problem is how to read it in a loop and check because if I am
doing something like this:

while(((line=file.ReadLine())!=null)&&(line.IndexO f("abc")==-1))

I will read additional line which doesn't consit abc ,how to avoid this?
Thanks a lot!

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
juli jul 写道:
Hello,
I have a text file and I have to check if the line I read contains some
word,the problem is how to read it in a loop and check because if I am
doing something like this:

while(((line=file.ReadLine())!=null)&&(line.IndexO f("abc")==-1))

I will read additional line which doesn't consit abc ,how to avoid this?
Thanks a lot!

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

but it works ok on my computer!
what's the content of your data file?
Nov 16 '05 #2

P: n/a
Why not have something like this:

// open the file
string filename = "abc.txt";
sting textToFind = "qwerty";
StreamReader fileReader = File.OpenText ( filename );
string currentLine = "";

// read each line from the file
while ( (currentLine = fileReader.ReadLine() ) != null )
{
if ( currentLine.IndexOf ( textToFind ) >= 0 )
{
// perform some action now you've found the text

// finish looking for the file
break;
}
}

// close the stream reader
fileReader.Close();

"juli jul" <ju******@yahoo.com> wrote in message
news:OF**************@TK2MSFTNGP10.phx.gbl...
Hello,
I have a text file and I have to check if the line I read contains some
word,the problem is how to read it in a loop and check because if I am
doing something like this:

while(((line=file.ReadLine())!=null)&&(line.IndexO f("abc")==-1))

I will read additional line which doesn't consit abc ,how to avoid this?
Thanks a lot!

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #3

P: n/a
hi Juli

while(((line=file.ReadLine())!=null)&&(line.IndexO f("abc")==-1))
the only reason I could think of for this not to stop at the correct line
is that it checks the index of condition (line.IndexOf("abc")==-1) before
the fist condition of (line=file.ReadLine())!=null) thus you read an
extra line
try
while (line=file.ReadLine())!=null){

if (line.IndexOf("abc")==-1) break ;
}

I believe this would solve the problem

Mohamed M .Mahfouz
Developer Support Engineer
ITWorx on behalf of Microsoft EMEA GTSC

Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.