如何检查从文件中读取的char是"/ n"?

Ale*_*erd 0 c# string

嗨,我正试图通过文件和计数行数,每行最大空格数和最长行.

如果我通过char给定文件迭代char,我如何确定"/ n"字符?

非常感谢.

这是我用于此的代码:

    using (StreamReader sr = new StreamReader(p_FileName))
     {

  char currentChar;
  int current_length=0,current_MaximumSpaces=0;
    p_LongestLine=0;
    p_NumOfLines=0;
    p_MaximumSpaces=0;
    while (!sr.EndOfStream){
        currentChar=Convert.ToChar(sr.Read());
        current_length++;
        if(Char.IsWhiteSpace(currentChar) || currentChar==null){
            current_MaximumSpaces++;
        }
        if(currentChar == '\n'){
            p_NumOfLines++;
        }
        if(current_length>p_LongestLine){
            p_LongestLine=current_length;
        }
        if(current_MaximumSpaces>p_MaximumSpaces){
            p_MaximumSpaces=current_MaximumSpaces;
        }
        current_length=0;
        current_MaximumSpaces=0;
    }
    sr.Close();
}
Run Code Online (Sandbox Code Playgroud)

jue*_*n d 5

if(currentChar == '\n')
    count++;
Run Code Online (Sandbox Code Playgroud)

  • 包含解析代码片段和正在解析的文件可能会有所帮助.我要问的第一个问题 - 您确定文件中的行是否以换行符终止? (5认同)