Using streamreader to read '//' lines
- How do Using Streamreader to read starts with this '//' line from a Text file and omit this line to move the next line position?
Any one pls to give the solution for this problem?
Réponses
- Hi Rajan,
I assume your requirement is to read a file line by and line and ignore lines starting with "\\".
Hope this helps.public void Read() { using (StreamReader Reader = new StreamReader(@"FileToRead.txt")) { string Line; while ((Line = Reader.ReadLine()) != null) { if (Line.TrimStart().StartsWith(@"\\")) continue; Console.WriteLine(Line); } } }
Thanks
PKR- Marqué comme réponseRudedog2Modérateurmercredi 4 novembre 2009 18:01
- Hi,
I hope it will help you
public void ReadFile() { using(StreamReader rdr = new StreamReader(@"C:\\file.txt")) { string line; while ((line = rdr.ReadLine()) != null) { if (!line.Contains(@"\\")) { Console.WriteLine(line); //Do Some thing } } } }
Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you- Marqué comme réponseRudedog2Modérateurmercredi 4 novembre 2009 18:01
Can't you write a wrapper class. Call ReadLine() on this? But if you even other methods then it is difficult.
I am thinking of method doing the same taking a StreamReader as parameter and doing operation on it.Class MyStreamReader() { StreamReader myReader; public MyStreamReader() { myReader = new StreamReader(); } public void ReadLine() { string Line; while ((Line = myReader .ReadLine()) != null) { if (!Line.TrimStart().StartsWith(@"\\")) { //DoStuff } } } public void Close() {myReader.Close();} public StreamReader Stream{get{return myReader ;}} }- Marqué comme réponseRudedog2Modérateurmercredi 4 novembre 2009 18:02
- Hi RajanCRT,
Thank you for your post.
As far as I know, for Override modifier, we cannot override a non-virtual or static method. The overridden base method must be virtual, abstract, or override.
Please check: http://msdn.microsoft.com/en-us/library/ebca9ah3.aspx
Thus, for your concern, you should write your own class to achieve your requirement. Gnanadurai has posted good sample code. Please have a try.
Hope this helps! If you have any concern, please feel free to let me know. :)
Best regards,
Yichun Chen
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.- Marqué comme réponseRudedog2Modérateurmercredi 4 novembre 2009 18:02
Toutes les réponses
- Hi Rajan,
I assume your requirement is to read a file line by and line and ignore lines starting with "\\".
Hope this helps.public void Read() { using (StreamReader Reader = new StreamReader(@"FileToRead.txt")) { string Line; while ((Line = Reader.ReadLine()) != null) { if (Line.TrimStart().StartsWith(@"\\")) continue; Console.WriteLine(Line); } } }
Thanks
PKR- Marqué comme réponseRudedog2Modérateurmercredi 4 novembre 2009 18:01
- Hi,
I hope it will help you
public void ReadFile() { using(StreamReader rdr = new StreamReader(@"C:\\file.txt")) { string line; while ((line = rdr.ReadLine()) != null) { if (!line.Contains(@"\\")) { Console.WriteLine(line); //Do Some thing } } } }
Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you- Marqué comme réponseRudedog2Modérateurmercredi 4 novembre 2009 18:01
- Hallo GnanaDurai...
Exactly u rcorrect. but in my application i have to read thes all the lines. b4 that here three regions r there after this
while
(!ConverterDefFileReadStream.EndOfStream)
{
...
region1
if(strLineText.ToUpper() == ("[parameters ]")
....for(i<0...)
..
strLineText = ConverterDefFileReadStream.ReadLine();
...
region2
if
(strLineText.ToUpper() == "[Counters]")
....for(i<0...)
..
strLineText = ConverterDefFileReadStream.ReadLine();
....
region3
for{
strLineText = ConverterDefFileReadStream.ReadLine();
}
...
}
in this three regions any one line having "//" (Comment) omitting that line and continue from that next line. in between this three regions any one line having the comment "//".
we can't using this to check all the in between this. so can we write using the class file to subclass and omit this line in overall? It is possible?
Have u got my last one...?
I think u have got it....
Thank u very much for ur replying already.... and waiting for this.
Thanks and regards
C.Rajandra Thilahar. - Hi,
Really can not understand your requirement could you please elaborate more about your requirement.
Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you - I want to override the Streamreader class and derived a new class to write a Program for omitting the Characters like "//".
Bcas - in my Project that 'Read line' function calls many places. so we cannot check all that places having the symbols. So commonly write one class for omit this symbols in any of the line by call this class.
http://www.daniweb.com/forums/post1036126.html#post1036126 --related to like this.
Have u got some idea now...?
Thanks for ur help....
CRT. - Hi RajanCRT,
Thank you for your post.
As far as I know, for Override modifier, we cannot override a non-virtual or static method. The overridden base method must be virtual, abstract, or override.
Please check: http://msdn.microsoft.com/en-us/library/ebca9ah3.aspx
Thus, for your concern, you should write your own class to achieve your requirement. Gnanadurai has posted good sample code. Please have a try.
Hope this helps! If you have any concern, please feel free to let me know. :)
Best regards,
Yichun Chen
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.- Marqué comme réponseRudedog2Modérateurmercredi 4 novembre 2009 18:02
Can't you write a wrapper class. Call ReadLine() on this? But if you even other methods then it is difficult.
I am thinking of method doing the same taking a StreamReader as parameter and doing operation on it.Class MyStreamReader() { StreamReader myReader; public MyStreamReader() { myReader = new StreamReader(); } public void ReadLine() { string Line; while ((Line = myReader .ReadLine()) != null) { if (!Line.TrimStart().StartsWith(@"\\")) { //DoStuff } } } public void Close() {myReader.Close();} public StreamReader Stream{get{return myReader ;}} }- Marqué comme réponseRudedog2Modérateurmercredi 4 novembre 2009 18:02
- HI.. Thanks for every one(whom all care to answer me)....I found the solution finally. Gnandurai and Vic vega ur answers also same as nearer to my answer.Soln is take the subclass for streamreader like below --Class IgnoreCommentsStrmRdr:StreamReader{... Readline();method() to call the Read line here form another one Program.now only use ur programs do omit the "//" symbol.}This is for common to any one function using to omit the Character using to read the TextStreamReader.Once again thanks to all.C.Rajandra Thilahar(CRT).
- Hi RajanCRT,
It's my pleasure. And thank you for your kindly sharing!
Have a great day!
Best regards,
Yichun Chen
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.

