I need to read from a CSV file byte by byte (Note: I don't want to read line by line). How can I detect if the byte that was read is a line break ? How to know that end of line was reached ?
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++;
}
}
New line character has decimal value 10
or hex value 0xA
. In order to check new line character compare the result against 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++;
}
}
When readByte
is equal to 10
or 0xA
in hex, the condition will be false.
Have a look at the ASCII Table for more information.
You might also want to define a constant like const int NEW_LINE = 0xA
and use that instead of just 0xA
in the while statement. This is just to help you figure out later what that 0xA
actually means.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With