Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
4.67/5 (2 votes)
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.

C#
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

You're doing
C#
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;
C#
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
 
Share this answer
 
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 :)
first you do in the while:
C#
while (sr.ReadLine() != null)
The pointer in the file moves to the next line. And then you do
C#
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[^]
 
Share this answer
 
Comments
sagar wasule 18-Apr-12 4:47am    
thanks
P.Salini 18-Apr-12 5:01am    
5 :)
Herman<T>.Instance 18-Apr-12 5:08am    
thank you
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:
C#
while (sr.Peek() >= 0)
{
   line = sr.ReadLine();
   //..
}
 
Share this answer
 
Comments
P.Salini 18-Apr-12 5:00am    
5 :)
CPallini 18-Apr-12 5:06am    
Thank you.
The problem is that you are overwriting each result in your loop:
C#
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:
C#
string[] allLines = File.ReadAllLines(fileName);
 
Share this answer
 
Comments
sagar wasule 18-Apr-12 4:47am    
thanks
P.Salini 18-Apr-12 5:00am    
5
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;
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900