C#控制台应用程序 - 奇怪的行为 - char'\ a'

时间:2010-12-24 17:32:30

标签: c# special-characters

在对应用程序进行大量调试之后,我注意到在搜索char'\ a'的文本时,控制台窗口会挂起。目标是从文件中删除字符。下面的部分只打印剥离的输出。它会导致同样的问题。

控制台窗口在退出程序时总是挂起,它会进入main的最后一个语句。我从switch语句中删除了'\ a',控制台应用程序不再挂起。知道为什么吗?我仍然需要删除char'\ a',但无法挂起来无法使应用程序正常工作。

    static void Main(string[] args)
    {
        string s_filename = args[0];
        Read(s_filename, 0);
    }

    static void Read(string s_filename, int i_char)

    {
            try
                {
                        char ch;
                        StringBuilder sb = new StringBuilder();
                        using (FileStream fs = new FileStream(s_filename, FileMode.Open, FileAccess.Read))
                        {
                            using (BinaryReader br = new BinaryReader(fs))
                            {
                                for (int i = 0; i < fs.Length; i++)
                                {
                                    byte b_byte = br.ReadByte();  //Reads the bytes one at a time.
                                    ch = (char)(b_byte);
                                    if (isString(ch, i_char) || (sb.Length > 0 && ch == ' '))
                                        sb.Append(ch);
                                    else
                                    {
                                        if (sb.Length == 0)
                                            continue;
                                        if (sb.Length >= 4)
                                        {
                                            Console.WriteLine(sb);
                                        }
                                        sb.Length = 0;
                                    }
                                }
                            }
                        }
                }
            catch (Exception e)
             {
                Console.WriteLine("Error {0}", e);
             }
    }

    static bool isString(char c, int i)  //http://msdn.microsoft.com/en-us/library/h21280bw.aspx
    {
        if (i == 0)
        {
            if (c >= 'a' && c <= 'z')
                return true;
            if (c >= 'A' && c <= 'Z')
                return true;
            if (c >= '0' && c <= '9')
                return true;
            switch (c)
            {
                case '~':
                case '`':
                case '!':
                case '@':
                case '#':
                case '$':
                case '%':
                case '^':
                case '&':
                case '*':
                case '(':
                case ')':
                case '-':
                case '_':
                case '+':
                case '=':
                case '[':
                case ']':
                case '{':
                case '}':
                case '|':
                case '\\':
                case ';':
                case ':':
                case '"':
                case '\'':
                case '<':
                case '>':
                case ',':
                case '.':
                case '?':
                case '/':
                case '\t': //Horizontal Tab
                case '\v': //Vertical Tab
                case '\n':  //Newline
                case '\f'://Formfeed
                case '\r': //carriage return
                case '\b': //Backspace
                case '\x7f': //delete character
                case '\x99': //TM Trademark
                case '\a': //Bell Alert
                    return true;
            }
        }
        if (i == 1)
        {
            if (c >= 'a' && c <= 'z')
                return true;
            if (c >= 'A' && c <= 'Z')
                return true;
            if (c >= '0' && c <= '9')
                return true;
            switch (c)
            {
                case '~':
                case '`':
                case '!':
                case '@':
                case '#':
                case '$':
                case '%':
                case '^':
                case '&':
                case '*':
                case '(':
                case ')':
                case '-':
                case '_':
                case '+':
                case '=':
                case '[':
                case ']':
                case '{':
                case '}':
                case '|':
                case '\\':
                case ';':
                case ':':
                case '"':
                case '\'':
                case '<':
                case '>':
                case ',':
                case '.':
                case '?':
                case '/':
                    return true;
            }
        }
        if (i == 2)
        {
            if (Char.IsLetterOrDigit(c))
                return true;
            switch (c)
            {
                case '~':
                case '`':
                case '!':
                case '@':
                case '#':
                case '$':
                case '%':
                case '^':
                case '&':
                case '*':
                case '(':
                case ')':
                case '-':
                case '_':
                case '+':
                case '=':
                case '[':
                case ']':
                case '{':
                case '}':
                case '|':
                case '\\':
                case ';':
                case ':':
                case '"':
                case '\'':
                case '<':
                case '>':
                case ',':
                case '.':
                case '?':
                case '/':
                    return true;
            }
        }
        if (i == 3)
        {
            if (Char.IsLetterOrDigit(c))
                return true;
            switch (c)
            {
                case '~':
                case '`':
                case '!':
                case '@':
                case '#':
                case '$':
                case '%':
                case '^':
                case '&':
                case '*':
                case '(':
                case ')':
                case '-':
                case '_':
                case '+':
                case '=':
                case '[':
                case ']':
                case '{':
                case '}':
                case '|':
                case '\\':
                case ';':
                case ':':
                case '"':
                case '\'':
                case '<':
                case '>':
                case ',':
                case '.':
                case '?':
                case '/':
                case '\t': //Horizontal Tab
                case '\v': //Vertical Tab
                case '\n':  //Newline
                case '\f'://Formfeed
                case '\r': //carriage return
                case '\b': //Backspace
                case '\x7f': //delete character
                case '\x99': //TM Trademark
                case '\a': //Bell Alert
                    return true;
            }
        }
        if (i == 4)
        {
            if (Char.IsLetterOrDigit(c))
                return true;
        }
        if (i == 5)
        {
            if (c >= 'a' && c <= 'z')
                return true;
            if (c >= 'A' && c <= 'Z')
                return true;
            if (c >= '0' && c <= '9')
                return true;
        }
        return false;
    }

2 个答案:

答案 0 :(得分:1)

我不明白为什么你的程序会挂起,但你没有给我们太多的继续。

也许尝试用文字7替换'\ a'。

有关其他字符代码,另请参阅http://asciitable.com/

但我认为它与其他一些程序逻辑有关,而不仅仅是代码中的'\a'

答案 1 :(得分:1)

无论如何,在我看来,这似乎是一种更好的方法 - 如果适用,请尝试重新编写代码:

using System.Linq;

bool Contains(string input)
{
    var arr = new[] { '\t', '\v', '\n', '\f', '\r', '\b', '\x7f', '\x99', '\a', .. };
    return arr.Any(c => input.Contains(c));
}
相关问题