Reading a binary file

Hello all. I have a major problem I have not been able to solve by simply googling “C# read binary file” and the like. I’m created a windows application that opens a binary file for reading, and displays the file in regular text into a multiline text box.

I have a binary file that contains rows of data. The data as it was created by the original program looks like the following…(The 4th column is a date that was not able to be copied for whatever reason).

[FONT=Calibri][SIZE=3]“adus”,“flex”,“46.0000000”,"",“2”[/SIZE]
[SIZE=3]“adus”,“fs”,“53.2500000”,"",“2”[/SIZE]
[SIZE=3]“adus”,“nok”,“190.0000000”,"",“2”[/SIZE]
[SIZE=3]“adus”,“nt”,“101.0000000”,"",“2”[/SIZE]
[SIZE=3]“adus”,“sne”,“284.7500000”,"",“2”[/SIZE]
[SIZE=3]“adus”,“vod”,“49.5000000”,"",“2”[/SIZE]
[SIZE=3]“cbus”,“035229bp7”,“88.8089000”,"",“2”[/SIZE]
[SIZE=3]“cbus”,“459200as0”,“88.5241000”,"",“2”[/SIZE]
[SIZE=3]“cbus”,“494368as2”,“86.6703000”,"",“2”[/SIZE]
[SIZE=3]“cbus”,“526055aa6”,“102.5000000”,"",“2”[/SIZE]
[SIZE=3]“cbus”,“549463ac1”,“88.0223000”,"",“2”[/SIZE]
[SIZE=3]“cbus”,“552676al2”,“92.0000000”,"",“2”[/SIZE]
[SIZE=3]“cbus”,“580135by6”,“86.8735000”,"",“2”[/SIZE]
[SIZE=3]“cbus”,“816591ae1”,“105.0000000”,"",“2”[/SIZE]
[SIZE=3]“cpus”,“aes.pr.c”,“61.6250000”,"",“2”[/SIZE]
[SIZE=3]“csus”,“adbe”,“67.2500000”,"",“2”[/SIZE]
[SIZE=3]“csus”,“aes”,“74.7500000”,"",“2”[/SIZE]
[SIZE=3]“csus”,“amr”,“67.0000000”,"",“2”[/SIZE]
[SIZE=3]“csus”,“aol”,“75.4375000”,"",“2”[/SIZE]
[SIZE=3]“csus”,“bud”,“70.8750000”,"",“2”[/SIZE]
[SIZE=3]“csus”,“c”,“55.5625000”,"",“2”[/SIZE]
[/FONT]
[FONT=Calibri][SIZE=3]As you can see the 2nd column is not of a fixed width. Also, the saved format of this data is a binary file with the extension .pri. I can read the first and second columns with no problem. The last three columsn diplay nothing but characters…The following is a representation of what I am able to do.[/SIZE][/FONT]
[FONT=Calibri][SIZE=3][/SIZE][/FONT]
adus flexG@???a
adus fs?J@???a
adus nok?g@???a
adus nt@Y@???a
adus sne?q@???a

My next post will contain the code I’m using to read this data and print it.


[SIZE=2][COLOR=#0000ff]private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][SIZE=2] outputFile()[/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][SIZE=2] temp;[/SIZE]
[SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] counter = 0;[/SIZE]
[SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] counter2 = 0;[/SIZE]
[SIZE=2]fileOutputPanel.Visible = [/SIZE][SIZE=2][COLOR=#0000ff]true[/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]fileOutput_txt.Text = file + [/SIZE][SIZE=2][COLOR=#a31515]"
"[/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2][COLOR=#2b91af]FileStream[/COLOR][/SIZE][SIZE=2] fs = [/SIZE][SIZE=2][COLOR=#2b91af]File[/COLOR][/SIZE][SIZE=2].Open(file, [/SIZE][SIZE=2][COLOR=#2b91af]FileMode[/COLOR][/SIZE][SIZE=2].Open);[/SIZE]
[SIZE=2][COLOR=#2b91af]BinaryReader[/COLOR][/SIZE][SIZE=2] myReader = [/SIZE][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][SIZE=2][COLOR=#2b91af]BinaryReader[/COLOR][/SIZE][SIZE=2](fs, [/SIZE][SIZE=2][COLOR=#2b91af]Encoding[/COLOR][/SIZE][SIZE=2].ASCII);[/SIZE]
[SIZE=2][COLOR=#0000ff]for[/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] i = 0; i < fs.Length; i++)[/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2][COLOR=#0000ff]try[/COLOR][/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2]temp = myReader.ReadChar().ToString();[/SIZE]
[SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][SIZE=2] (i > 15)[/SIZE]
[SIZE=2]{[/SIZE][SIZE=2][COLOR=#008000]//33[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][SIZE=2] (counter > 3 && counter2 <5)[/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2]fileOutput_txt.Text = fileOutput_txt.Text + [/SIZE][SIZE=2][COLOR=#a31515]"	"[/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2]fileOutput_txt.Text = fileOutput_txt.Text + temp;[/SIZE]
[SIZE=2]counter = counter + 1;[/SIZE]
[SIZE=2]counter2 = counter2 + 1;[/SIZE]
[SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][SIZE=2] (counter2 > 32)[/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2]fileOutput_txt.Text = fileOutput_txt.Text + [/SIZE][SIZE=2][COLOR=#a31515]"
"[/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]counter2 = 0;[/SIZE]
[SIZE=2]counter = 0;[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2][COLOR=#0000ff]catch[/COLOR][/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2]fileOutput_txt.Text = fileOutput_txt.Text + [/SIZE][SIZE=2][COLOR=#a31515]"	"[/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]temp = myReader.ReadSingle().ToString();[/SIZE]
[SIZE=2]i = i + 4;[/SIZE]
[SIZE=2]fileOutput_txt.Text = fileOutput_txt.Text + temp;[/SIZE]
[SIZE=2]counter2 = counter2 + 1;[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2]}[/SIZE]

I’ve tried ReadSingle(), ReadChar(), Read(), ReadByte(), ReadChars(), ReadInt16, 32, 64, etc etc. The binary file is created by a program called Axys and has to do with stocks and trading them. So I do not have access to their write method.

I don’t remember the exact implementation, but you have to read it into a structure and type cast to a char pointer (if I am not mistaken).

Thanks for your help. I’m not quite sure what you mean by reading it into a structure and then type cast it to a character pointer…Do you mean read in those bytes into an array or?? Any and all help would be appreciated!

Sorry, I was thinking C++. I will post back with more thoughts