Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if a byte is a line break when reading from a CSV file byte by byte - C#

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++;
    }
} 
like image 890
Danielle Avatar asked Mar 12 '23 01:03

Danielle


1 Answers

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.

UPDATE

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.

like image 117
Husein Roncevic Avatar answered Apr 07 '23 10:04

Husein Roncevic