Results 1 to 6 of 6

Thread: Reading a binary file.

  1. #1

    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).

    "adus","flex","46.0000000","","2"
    "adus","fs","53.2500000","","2"
    "adus","nok","190.0000000","","2"
    "adus","nt","101.0000000","","2"
    "adus","sne","284.7500000","","2"
    "adus","vod","49.5000000","","2"
    "cbus","035229bp7","88.8089000","","2"
    "cbus","459200as0","88.5241000","","2"
    "cbus","494368as2","86.6703000","","2"
    "cbus","526055aa6","102.5000000","","2"
    "cbus","549463ac1","88.0223000","","2"
    "cbus","552676al2","92.0000000","","2"
    "cbus","580135by6","86.8735000","","2"
    "cbus","816591ae1","105.0000000","","2"
    "cpus","aes.pr.c","61.6250000","","2"
    "csus","adbe","67.2500000","","2"
    "csus","aes","74.7500000","","2"
    "csus","amr","67.0000000","","2"
    "csus","aol","75.4375000","","2"
    "csus","bud","70.8750000","","2"
    "csus","c","55.5625000","","2"

    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.

    adus flexG@????
    adus fs?J@????
    adus nok?g@????
    adus nt@Y@????
    adus sne?q@????

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

  2. #2
    Code:
    privatevoid outputFile()
    {
    string temp;
    int counter = 0;
    int counter2 = 0;
    fileOutputPanel.Visible = true;
    fileOutput_txt.Text = file + "\r\n";
    FileStream fs = File.Open(file, FileMode.Open);
    BinaryReader myReader = newBinaryReader(fs, Encoding.ASCII);
    for (int i = 0; i < fs.Length; i++)
    {
    try
    {
    temp = myReader.ReadChar().ToString();
    if (i > 15)
    {//33
    if (counter > 3 && counter2 <5)
    {
    fileOutput_txt.Text = fileOutput_txt.Text + "\t";
    }
    fileOutput_txt.Text = fileOutput_txt.Text + temp;
    counter = counter + 1;
    counter2 = counter2 + 1;
    if (counter2 > 32)
    {
    fileOutput_txt.Text = fileOutput_txt.Text + "\r\n";
    counter2 = 0;
    counter = 0;
    }
    }
    }
    catch
    {
    fileOutput_txt.Text = fileOutput_txt.Text + "\t";
    temp = myReader.ReadSingle().ToString();
    i = i + 4;
    fileOutput_txt.Text = fileOutput_txt.Text + temp;
    counter2 = counter2 + 1;
    }
    }
    }
    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.
    Last edited by raithedavion; July 7th, 2008 at 12:58 PM.

  3. #3
    actionAction's Avatar
    1,142
    posts
    humanBeing._beard=true;
    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).

  4. #4
    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!

  5. #5
    actionAction's Avatar
    1,142
    posts
    humanBeing._beard=true;
    Sorry, I was thinking C++. I will post back with more thoughts

  6. #6

    You have to check the encoding

    As far as I see, here is an issue related to encoding; you have to know the Encoding of the file (or it's Code Page in order to get the encoding) and after that, read from file using the encoding, like:

    I would write more here but the editor from kirupa sucks big time, it almost drive me crazy.
    So, your quotes are transformed because of the usage of a wrong encoding.

    using (TextReader input = new StreamReader(new FileStream("SomeFileName", FileMode.Open), System.Text.Encoding.UTF8))
    {
    String str = input.ReadToEnd();
    }



    Cheers

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012