如何在逐字节读取 CSV 文件时检测字节是否为换行符 - C#
How to detect if a byte is a line break when reading from a CSV file byte by byte - C#
我需要逐字节读取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++;
}
}
换行符有十进制值 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++;
}
}
当 readByte
等于 10
或 0xA
(十六进制)时,条件将为假。
查看 ASCII Table 了解更多信息。
更新
您可能还想定义一个像 const int NEW_LINE = 0xA
这样的常量,并在 while 语句中使用它而不是 0xA
。这只是为了帮助您稍后弄清楚 0xA
的实际含义。
我需要逐字节读取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++;
}
}
换行符有十进制值 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++;
}
}
当 readByte
等于 10
或 0xA
(十六进制)时,条件将为假。
查看 ASCII Table 了解更多信息。
更新
您可能还想定义一个像 const int NEW_LINE = 0xA
这样的常量,并在 while 语句中使用它而不是 0xA
。这只是为了帮助您稍后弄清楚 0xA
的实际含义。