Click to See Complete Forum and Search --> : ile handling problem


maifs
July 13th, 2009, 04:04 PM
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

BigEd781
July 13th, 2009, 04:37 PM
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").

maifs
July 13th, 2009, 04:53 PM
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 .