Click here to Skip to main content
15,917,617 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys
I am trying to read some texts from txt file line by line and for example print it in console window, so I write these codes :

C#
namespace test1
{
    class Program
    {
        static void Main(string[] args)
        {
            
            StreamReader A;
            A = new StreamReader("a.txt");
            string line = A.ReadLine();
            while (line!=null)
            {
                Console.WriteLine(line);
            }
            Console.ReadKey();
            
        }
    }
}


my txt file contains 3 line of text : 1- ali goes to school 2- water is hot 3- I am tired
so when run the program console windows show up and print the first line of text repetitive and don't stop until I close that.
I have two problem: first why this program reads only line1 of my text and don't read other lines, second why this won't stop printing


thanks everyone
Posted

Your code should be
C#
namespace test1
{
    class Program
    {
        static void Main(string[] args)
        {
            
           StreamReader A;
            A = new StreamReader("A.txt");           
            
            while (!A.EndOfStream)
            {
                Console.WriteLine(A.ReadLine());
            }
            Console.ReadKey();
            
        }
    }
}
 
Share this answer
 
Use the proper function instead of re-inventing the wheel: see File.ReadAllLines(...)[^].
Andi

PS: In your example, you don't read the next line inside the loop.
 
Share this answer
 
v2
Comments
Thomas Daniels 28-Mar-13 7:05am    
+5!
Andreas Gieriet 28-Mar-13 8:44am    
Thanks for your 5!
Andi
If I were you, I would try :

C#
string line;
while ((line = A.ReadLine()) != null)
{
   Console.WriteLine(line);
}


Your problem is you create an infinite loop in your while block ; you have to modify your line variable inside the loop if you want a chance to escape it sometime.

Hope this helps.
 
Share this answer
 
v2
Comments
Thomas Daniels 28-Mar-13 7:07am    
I voted 4 because this works, but it's easier to store all lines in a string array using File.ReadAllText, and then iterate over the lines (see Solution 2).
phil.o 28-Mar-13 7:39am    
OK, thanks :)
I think that understanding why it was not working in the first place is as important for the OP as having an easier solution.
Thomas Daniels 28-Mar-13 8:15am    
I understand, thanks to clarify that!
Andreas Gieriet 28-Mar-13 8:53am    
My 5 for your explanation.
It's a matter of knowing the .Net library. For the tiny data set as suggested in the sample code, I vote for File.ReadAllLines. For huge data, line-wise processing is advided, though.
And the OP could have searched for Google: C# text file read line and would have found the top hit How to: Read Text from a File which shows more or less your solution...
Cheers
Andi
ali_crash 28-Mar-13 7:43am    
thanks man
helped me a lot :D

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