CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2009
    Posts
    2

    ile handling problem

    i want to fetch the characters from file for example :

    1st example:

    // mehmood// hello ,how r u?

    just within comments and replace it to all caps

    2nd example:

    /* hi
    mehmood
    ahmed */


    i want to fetch the characters within sigle line comment and multi line comments .

    just within comments not outside the comment.

    --------------------------------------------------------------------------------

    i am trying this code, kindly correct my code:

    FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);

    StreamReader sr = new StreamReader(fs);
    int countComment=0;
    while(sr.Peek() >= 0 )
    {
    str =sr.ReadLine();
    strTemp = new char[str.Length];
    #region From String method
    int count=0;
    for (int i = 0; i < str.Length; i++)
    {
    if (str[i] == '/')
    {
    ++countComment;
    count=i;
    }
    if (char.IsLetterOrDigit(str,count+1) && countComment==2)
    {
    strTemp2 += str.Substring(count+1).ToUpper();
    }

    }
    #endregion


    countComment = 0;
    this.textBox2.Text += strTemp2 + Environment.NewLine; ;
    this.textBox1.Text += str + Environment.NewLine;
    }
    sr.Close();
    fs.Close();



    it generates an exception :

    System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
    Parameter name: index

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: ile handling problem

    I would suggest learning how to use regular expressions. Short of that, your code is hard to read as you are not wrapping it in [ code][ /code] blocks and you haven't told us what the actual problem is (just, "fix my code").

  3. #3
    Join Date
    Jan 2009
    Posts
    2

    Re: ile handling problem

    Code:
     FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
               
                StreamReader sr = new StreamReader(fs);
                int countComment=0,countMulti=0;
                while(sr.Peek() >= 0 )
                {
                    str =sr.ReadLine();
                  
                    #region From String method
                    int count=0;
                    for (int i = 0; i < str.Length; i++)
                    {
                        if (str[i] == '/')
                        {
                            ++countComment;          
                            count=i;
                            if (str[i] =='*')
    	                    {
    		                     ++countMulti;
    	                    }                    	                      
                            if (countComment == 2)
                            {
                                strTemp2 += str.Substring(count + 1).ToUpper();
                                break;
                            }
                            if (countComment == 1 && countMulti == 1)
                            {
                                strTemp2 += str.Substring(count + 1).ToUpper();
                                break;
                            }
                        }
                                      
                        
                    }
                    #endregion
                    countComment = 0;
                    this.textBox2.Text = strTemp2 + Environment.NewLine; ;
                    this.textBox1.Text += str + Environment.NewLine;
                }
                sr.Close();
                fs.Close()
    ------------------------------------------------------------------------
    input file :
    ---------------------------------------------
    // mehmood// hello ,how r u?
    /* hi
    mehmood
    ahmed */

    i want take output as:
    MEHMOOD//HELLO,HOW R U?
    HI MEHMOOD AHMED

    I did first line .

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured