Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.


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


    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
    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++)
    temp = myReader.ReadChar().ToString();
    if (i > 15)
    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;
    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
    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
    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();


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


Copyright 1999 - 2012