locked
Binary Read RRS feed

  • Question

  • Hi, 

    I need to binary read a file from a file 'file.dat'.

    I did a binary write and that works fine. This is how I did it:

    binaryWrite(data);

    binaryWrite("\r\n");

    I need the \r\n to ensure my new data is on the second line. However, when I binary read the data, I don't want the \r\n.

    How do I do it?

    (Windows Form)

    Monday, May 18, 2020 5:43 AM

All replies

  • You are mixing things. You are using both binary and non-binary on the same file. The "\r\n" results in bytes 13 and 10 (decimal) in the byte stream, and these bytes are legal inside binary data. For instance, you binary data could include a short integer whose value is 2573, which would produce the same bytes as \r\n.

    So you have to decide on how you want to write your file. If you decide to use binary, then you have to read it by specifying a length of information to read, not a specific marker on the file (such as \r\n), since all markers would be valid within the byte stream.

    Or, if you decide to use a text file, you can serialize your data into a text format such as xml or json, and then you read it back by using deserialization. But don't mix binary with text markers in the same file.

    Monday, May 18, 2020 5:53 AM