Click here to Skip to main content
14,641,306 members
Rate this:
Please Sign up or sign in to vote.
See more:
I was reading a text file in C# by code as given below , ideally this should read each and every line one by one but the problem is every alternate line is skipped while reading , I'm not able to understand what the problem is . Please can any help with any solution.

string fileName = @"C:\NewTextDoc.txt";

string str = string.Empty;

if (File.Exists(fileName))
{
    StreamReader sr = new StreamReader(fileName);
    String line;

    while (sr.ReadLine() != null)
    {
        line = sr.ReadLine();
        if (line.Trim() != string.Empty)
        {
           s = line.substring(1,4) ;
        }
    }
}
else
{
    s = "File does not exists";
}
return s;
Posted
Updated 17-Apr-12 22:42pm
v2
Rate this:
Please Sign up or sign in to vote.

Solution 1

You're doing
while(sr.ReadLine() != null)


That consumes a line, then you're doing line = sr.ReadLine(); again, that consumes another line, but only the second read is stored in sr.

You could change it to this;
while ((line = sr.ReadLine()) != null)
{
  if (line.Trim() != string.Empty)
  {
    s = line.substring(1,4) ;
  }
}


Or maybe just use FileInfo.ReadAllLines unless your file is absolutely massive in size.

Hope this helps,
Fredrik
   
Comments
sagar wasule 18-Apr-12 4:47am
   
thanks
Fredrik Bornander 18-Apr-12 5:03am
   
Happy to help.
P.Salini 18-Apr-12 5:01am
   
5 :)
Fredrik Bornander 18-Apr-12 5:03am
   
Why thank you good sir :)
Rate this:
Please Sign up or sign in to vote.

Solution 2

first you do in the while:
while (sr.ReadLine() != null)
The pointer in the file moves to the next line. And then you do
line = sr.ReadLine();
so the pointer is moved again. which brings you to the while...
That declares while you think you read halve the file.

do you need to read the file line by line?
You can read it very simple according to MSDN[^]
   
Comments
sagar wasule 18-Apr-12 4:47am
   
thanks
P.Salini 18-Apr-12 5:01am
   
5 :)
digimanus 18-Apr-12 5:08am
   
thank you
Rate this:
Please Sign up or sign in to vote.

Solution 5

You are actually calling StreamReader.Readline method twice (both in the while condition and in the line = sr.ReadLine; statement, but using only one of the return values).
As in the documentation[^] code sample, you may use StreamReader.Peek method to check for the End Of File condition:
while (sr.Peek() >= 0)
{
   line = sr.ReadLine();
   //..
}
   
Comments
P.Salini 18-Apr-12 5:00am
   
5 :)
CPallini 18-Apr-12 5:06am
   
Thank you.
Rate this:
Please Sign up or sign in to vote.

Solution 4

The problem is that you are overwriting each result in your loop:
s = line.substring(1,4) ;

So all that you will get is the last non-blank line in the file.

Depending on what you are trying to achieve, have you looked at using:
string[] allLines = File.ReadAllLines(fileName);
   
Comments
sagar wasule 18-Apr-12 4:47am
   
thanks
P.Salini 18-Apr-12 5:00am
   
5
Rate this:
Please Sign up or sign in to vote.

Solution 6

Try this code.
string fileName = @"C:\NewTextDoc.txt";

string str = string.Empty;

if (File.Exists(fileName))
{
StreamReader sr = new StreamReader(fileName);
String line;

while ((line = sr.ReadLine()) != null)
{
if (line.Trim() != string.Empty)
{
s = line.substring(1,4) ;
}
}
}
else
{
s = "File does not exists";
}
return s;
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100