Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 :
 
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 28-Mar-13 1:57am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
v2
Comments
ProgramFOX at 28-Mar-13 7:05am
   
+5!
Andreas Gieriet at 28-Mar-13 8:44am
   
Thanks for your 5!
Andi
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

If I were you, I would try :
 
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.
  Permalink  
v2
Comments
ProgramFOX at 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 at 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.
ProgramFOX at 28-Mar-13 8:15am
   
I understand, thanks to clarify that!
Andreas Gieriet at 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 at 28-Mar-13 7:43am
   
thanks man
helped me a lot :D
phil.o at 28-Mar-13 7:49am
   
Thanks!
As I told before, other solutions are more straightforward ; but it's also important for you to understand the reason of your problem.
I hope you will apply this acquired knowledge in your future codings.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Your code should be
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();
            
        }
    }
}
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 277
1 Maciej Los 230
2 BillWoodruff 205
3 Sergey Alexandrovich Kryukov 195
4 DamithSL 165
0 OriginalGriff 5,130
1 DamithSL 4,157
2 Maciej Los 3,650
3 Kornfeld Eliyahu Peter 3,460
4 Sergey Alexandrovich Kryukov 2,811


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 29 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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