Q: about StreamReader

hi,

i want to know the total number of lines in the document because i will use it as iteration statment like or example

for(i=0, i < totallinenumber, i++)

myarrayIdea = mystreamreader;

is there any way to do that or at least to get the lines to my array

also i want to know if the streamreader riched to the end of document or not

thx




Answer this question

Q: about StreamReader

  • renz_wales

    Close is called in my example as well. Because the StreamReader is in a using statement .Dispose(); will be called after the using statement. And .Dispose(); will call close when needed.


  • p4praveen

    You can simply read line with a StreamReader.ReadLine, this will give you a string when available; otherwise null. When you get a null value back, you know the end of the file it reached.


    using(StreamReader reader = new StreamReader(@"c:\myfile.txt"))
    {
    string line = null;
    int lineCounter = 0;

    while( (line = reader.ReadLine()) != null)
    {
    // Higher the line counter with 1.
    lineCounter++;

    // TODO: Do something with the readed line.
    }
    }




  • Stefano Straus

    hi,

    many thx it working fine

    i just wondering , every tutorial i saw it was closing the stream reader like for example reader.close(); but when i tried to close this stream it maked it as error

    and also can i just use this as a counter without doing anything with data or the data will be in computer memory

    i'm asking this for one reason i have 2 files in different languages (one english, other one arabic) i want to put them in a table (line number, arabic , english) 

    best regards

     



  • Q: about StreamReader