After extensive debugging of an application, I noticed the console window would hang when searching text for the char '\a'. The goal is to strip out characters from a file. The below portion just prints the stripped output. It causes the same issue.
The console window would always hang upon exiting the program, and it would make it to the last statement of main. I removed the '\a' from the switch statement and the console application does not hang anymore. Any idea why? I still need to strip out the char '\a', but cannot get the application to work without hanging.
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 '|':
开发者_StackOverflow 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;
}
I don't see why your program would hang, but you haven't given us much to go on.
Maybe try replacing the '\a' with a literal 7.
See also http://asciitable.com/ for other character codes.
But I'm thinking it has to do with some other program logic, not just the '\a'
in your code.
Anyway, that seems to me a better approach - try to rework your code if applicable:
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));
}
精彩评论