如何在读取文件时忽略/ * comment * /的读取内容

时间:2016-12-29 10:08:34

标签: c# console-application

以下是我的代码:

string ckeywords = File.ReadAllText("E:\\ckeywords.csv");
string[] clines = File.ReadAllLines("E:\\cprogram\\cpro\\bubblesort.c");
string letters="";

foreach(string line in clines)
{
    char[] c = line.ToCharArray();
    foreach(char i in c)
    {
        if (i == '/' || i == '"')
        {
            break;
        }
        else 
        {
            letters = letters + i;
        }
    }
}
letters = Regex.Replace(letters, @"[^a-zA-Z ]+", " ");

List<string> listofc = letters.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList();
List<string> listofcsv = ckeywords.Split(new char[] { ',', '\t', '\n', ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(p => p.Trim()).ToList();
List<string> Commonlist = listofcsv.Intersect(listofc).ToList();

使用此if条件,我可以忽略单行注释和内容之间的读取内容(&#34;&#34;)。

我需要忽略阅读多行注释的内容。我应该使用哪种条件? 假设我的.c文件有这一行注释,所以上面的代码我不知道如何从/ *开始迭代到* /并忽略它们之间的内容。

/ * printf(&#34;按升序排序列表:\ n&#34;);

for(c = 0; c&lt; n; c ++)    printf(&#34;%d \ n&#34;,array [c]); * /

2 个答案:

答案 0 :(得分:2)

我成功地解决了我的问题现在我可以在不使用正则表达式的情况下以更简单的方式忽略读取/ * * /的内容。 这是我的代码:

string[] clines = File.ReadAllLines("E:\\cprogram\\cpro\\bubblesort.c");
List<string> list = new List<string>();
int startIndexofcomm, endIndexofcomm;

 for (int i = 0; i < clines.Length ; i++ )
    {
       if (clines[i].Contains(@"/*"))
          {
             startIndexofcomm = clines[i].IndexOf(@"/*");
             list.Add(clines[i].Substring(0, startIndexofcomm));

             while(!(clines[i].Contains(@"*/")))
             {
                i++;
             }

             endIndexofcomm = clines[i].IndexOf(@"*/");
             list.Add(clines[i].Substring(endIndexofcomm+2));

             continue;
          }
          list.Add(clines[i]);
     }

答案 1 :(得分:0)

这是天真地执行以下操作的代码:

  1. 它删除了以/*开头并以*/开头的所有多行评论,即使两者之间有换行符也是如此。
  2. 删除以//开头并以行尾结尾的任何单行评论
  3. 如果它们位于以"开头且以"结尾的字符串中,则会删除上述任何评论。
  4. LINQPad代码:

    void Main()
    {
        var code = File.ReadAllText(@"d:\temp\test.c");
        code.Dump("input");
    
        bool inString = false;
        bool inSingleLineComment = false;
        bool inMultiLineComment = false;
    
        var output = new StringBuilder();
        int index = 0;
    
        while (index < code.Length)
        {
            // First deal with single line comments: // xyz
            if (inSingleLineComment)
            {
                if (code[index] == '\n' || code[index] == '\r')
                {
                    inSingleLineComment = false;
                    output.Append(code[index]);
                    index++;
                }
                else
                    index++;
    
                continue;
            }
    
            // Then multi-line comments: /* ... */
            if (inMultiLineComment)
            {
                if (code[index] == '*' && index + 1 < code.Length && code[index + 1] == '/')
                {
                    inMultiLineComment = false;
                    index += 2;
                }
                else
                    index++;
                continue;
            }
    
            // Then deal with strings
            if (inString)
            {
                output.Append(code[index]);
                if (code[index] == '"')
                    inString = false;
                index++;
                continue;
            }
    
            // If we get here we're not in a string or in a comment
            if (code[index] == '"')
            {
                // We found the start of a string
                output.Append(code[index]);
                inString = true;
                index++;
            }
            else if (code[index] == '/' && index + 1 < code.Length && code[index + 1] == '/')
            {
                // We found the start of a single line comment
                inSingleLineComment = true;
                index++;
            }
            else if (code[index] == '/' && index + 1 < code.Length && code[index + 1] == '*')
            {
                // We found the start of a multi line comment
                inMultiLineComment = true;
                index++;
            }
            else
            {
                // Just another character
                output.Append(code[index]);
                index++;
            }
        }
    
        output.ToString().Dump("output");
    }
    

    示例输入:

    This should be included // This should not
    This should also be included /* while this
    should not */ but this should again be included.
    
    Any comments in " /* strings */ " should be included as well.
    This goes for "// single line comments" as well.
    

    示例输出(请注意,下面某些行的末尾有一些空格是不可见的):

    This should be included 
    This should also be included  but this should again be included.
    
    Any comments in " /* strings */ " should be included as well.
    This goes for "// single line comments" as well.