2011-05-19 92 views
4

我的错误是什么,因为我无法在互联网上找到与我正在做的事情相匹配的例子,或者至少我不确定它是否会发生?阅读文件流读取器

我遇到的问题是它不喜欢

hexIn = fileStream.Read() 

代码:

FileStream fileStream = new FileStream(fileDirectory, FileMode.Open, FileAccess.Read); 
String s; 

try 
{ 
    for (int i = 0; (hexIn = fileStream.Read() != -1; i++) 
    { 
     s = hexIn.ToString("X2"); 
     //The rest of the code 
    } 
} 
finally 
{ 
    fileStream.Close(); 
} 
+4

“它不喜欢它”不是一个非常详细的描述。你有什么确切的问题? – 2011-05-19 02:29:05

回答

2

有几件事我会做出不同。

第一,你应该使用FileStreamusing。但实际上,如果你只是想读取文本文件中的行,一个StreamReader将是确定:

try 
{ 
    using (StreamReader sr = new StreamReader("TestFile.txt")) 
    { 
     String line; 

     while ((line = sr.ReadLine()) != null) 
     { 
      // convert line to Hex and then format with .ToString("X2") 
     } 
    } 
} 
catch 
{ 
    // handle error 
} 

如果你想你的整个输入文件转换为十六进制值,让我们知道。我现在只是假设一行一行。

6

缺少“)”。 。尝试:

using (StreamReader sr = new StreamReader("TestFile.txt")) 
{ 
    String line; 

    while ((line = sr.ReadLine()) != null) 
    { 
     s=... 
    } 
}