Dan*_*lle 4 c# csv arrays byte filestream
我需要逐字节读取 CSV 文件(注意:我不想逐行读取)。如何检测读取的字节是否为换行符?如何知道已到达行尾?
int count = 0;
byte[] buffer = new byte[MAX_BUFFER];
using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
// Read bytes form file until the next line break -or- eof
// so we don't break a csv row in the middle
// What should be instead of the 'xxx' ?
while (((readByte = fs.ReadByte()) != 'xxx') && (readByte != -1))
{
buffer[count] = Convert.ToByte(readByte);
count++;
}
}
Run Code Online (Sandbox Code Playgroud)
换行符有十进制值10或十六进制值0xA。为了检查换行符,将结果与0xA
int count = 0;
byte[] buffer = new byte[MAX_BUFFER];
using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
// Read bytes form file until the next line break -or- eof
// so we don't break a csv row in the middle
// What should be instead of the 'xxx' ?
while (((readByte = fs.ReadByte()) != 0xA) && (readByte != -1))
{
buffer[count] = Convert.ToByte(readByte);
count++;
}
}
Run Code Online (Sandbox Code Playgroud)
当readByte等于10或0xA以十六进制表示时,条件为假。查看ASCII 表以获取更多信息。
您可能还想定义一个常量 likeconst int NEW_LINE = 0xA并使用它,而不仅仅是0xA在 while 语句中。这只是为了帮助您稍后弄清楚这0xA实际上意味着什么。