read file

  • Thread starter Thread starter juli jul
  • Start date Start date
J

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.IndexOf("abc")==-1))

I will read additional line which doesn't consit abc ,how to avoid this?
Thanks a lot!
 
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.IndexOf("abc")==-1))

I will read additional line which doesn't consit abc ,how to avoid this?
Thanks a lot!
but it works ok on my computer!
what's the content of your data file?
 
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();
 
hi Juli

while(((line=file.ReadLine())!=null)&&(line.IndexOf("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
 
Back
Top