I'm trying to figure out something while reading a binary file.
On my file there's a long type value at the beginning ('8'), and then, depending on the file, the next value will be either a letter (for example 'E') or another long (for example '5'). My question is: How can I know what kind of data value I'm reading on my file?
My code looks like this:
FileStream streamR = new FileStream(archivo, FileMode.Open, FileAccess.Read);
BinaryReader reader = new BinaryReader(streamR);
Boolean checkNext = false;
Boolean bandHeadr = false;
Boolean bandRank = false;
long archivoPos;
while (reader.BaseStream.Position != reader.BaseStream.Length)
{
if (bandHeadr == false)
{
// HERE IT READS THE FIRST VALUE, ALWAYS A LONG TYPE VALUE
long header = reader.ReadInt64();
data.Add(header);
bandCabecera = true;
}
if (checkNext == false)
{
try
{
// HERE I'M TRYING TO CHECK THE NEXT VALUE, BUT RETURNS AN ASCII CODE IF IT IS A LETTER
int ix = reader.PeekChar();
}
catch
{
// THIS WILL IF THE NEXT VALUE IS ANOTHER LONG
if (bandRank == false)
{
try
{
long rang = reader.ReadInt64();
rangO = rang;
button9.Enabled = false;
}
catch
{
// EMPTY CATCH
}
bandRank = true;
}
}
checkNext = true;
}
}
I'm using PeekChar for the sake of not moving to the next position of the file.
TryParseand if it succeeds then you have it saved as a number if it fails then save it as achar.Switchstatement with patterns. MSDN