Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StreamReader and binary data

I have this text file what contains different fields. Some fields may contain binary data. I need to get all the data in the file but right now when using StreamReader then it wont read the binary data block and data what comes after that. What would be the best solution to solve this problem?

Example:

field1|field2|some binary data here|field3

Right now i read in the file like this:

public static string _fileToBuffer(string Filename)
{
    if (!File.Exists(Filename)) throw new ArgumentNullException(Filename, "Template file does not exist");

    StreamReader reader = new StreamReader(Filename, Encoding.Default, true);
    string fileBuffer = reader.ReadToEnd();
    reader.Close();

    return fileBuffer;
}

EDIT: I know the start and end positions of the binary fields.

like image 379
hs2d Avatar asked Jun 27 '11 10:06

hs2d


People also ask

What is StreamReader used for?

StreamReader is designed for character input in a particular encoding, whereas the Stream class is designed for byte input and output. Use StreamReader for reading lines of information from a standard text file. This type implements the IDisposable interface.

Which method is used for reading data in binary file?

To read from a binary file Use the ReadAllBytes method, which returns the contents of a file as a byte array.

What is StreamReader class in C#?

C# StreamReader class is used to read string from the stream. It inherits TextReader class. It provides Read() and ReadLine() methods to read data from the stream.

What is binary reader in C#?

The BinaryReader class provides methods that simplify reading primitive data types from a stream. For example, you can use the ReadBoolean method to read the next byte as a Boolean value and advance the current position in the stream by one byte. The class includes read methods that support different data types.


1 Answers

use BinaryReader

like image 199
abatishchev Avatar answered Sep 21 '22 03:09

abatishchev