Le réseau pour les développeurs > Forums - Accueil > Visual C# Language > Using streamreader to read '//' lines
Poser une questionPoser une question
 

TraitéeUsing streamreader to read '//' lines

  • mardi 3 novembre 2009 05:08RajanCRT Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    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

  • mardi 3 novembre 2009 05:18Vic Vega Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     TraitéeA du code
    Hi Rajan,
                  I assume your requirement is to read a file line by and line and ignore lines starting with "\\".
    		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);
    				}
    			}
    		}
    
    Hope this helps.
    Thanks
    PKR
  • mardi 3 novembre 2009 05:24Gnanadurai Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     TraitéeA du code
    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
  • mercredi 4 novembre 2009 13:59Vic Vega Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     TraitéeA du code

    Can't you write a wrapper class. Call ReadLine() on this? But if you even other methods then it is difficult.



    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 ;}}
                 
              
    }
    
    I am thinking of method doing the same taking a StreamReader as parameter and doing operation on it.
  • mercredi 4 novembre 2009 09:46YiChun ChenMSFT, ModérateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     Traitée
    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.

Toutes les réponses

  • mardi 3 novembre 2009 05:18Vic Vega Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     TraitéeA du code
    Hi Rajan,
                  I assume your requirement is to read a file line by and line and ignore lines starting with "\\".
    		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);
    				}
    			}
    		}
    
    Hope this helps.
    Thanks
    PKR
  • mardi 3 novembre 2009 05:24Gnanadurai Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     TraitéeA du code
    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
  • mardi 3 novembre 2009 13:10RajanCRT Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    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.

  • mercredi 4 novembre 2009 04:02Gnanadurai Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    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
  • mercredi 4 novembre 2009 04:23RajanCRT Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    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.
  • mercredi 4 novembre 2009 09:46YiChun ChenMSFT, ModérateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     Traitée
    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.
  • mercredi 4 novembre 2009 13:59Vic Vega Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     TraitéeA du code

    Can't you write a wrapper class. Call ReadLine() on this? But if you even other methods then it is difficult.



    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 ;}}
                 
              
    }
    
    I am thinking of method doing the same taking a StreamReader as parameter and doing operation on it.
  • mercredi 4 novembre 2009 17:51RajanCRT Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    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 Text
    StreamReader.

    Once again thanks to all.

    C.Rajandra Thilahar(CRT).
  • jeudi 5 novembre 2009 04:20YiChun ChenMSFT, ModérateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    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.