dcsimg
Results 1 to 5 of 5

Thread: How to find a particular string from a file written with binary writer

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2018
    Posts
    16

    How to find a particular string from a file written with binary writer

    Hello Everyone,

    actually i have an file in which data(no of integers,strings) has been written with binary writer.....but now i want read specific data from that file not from starting to end. for example i just want to read a string from that file which is written in between somewhere of that file and i also want to read the position of that string....please help.

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,583

    Re: How to find a particular string from a file written with binary writer

    Well, unless you know where the string is you're going to have to search for it.
    And while you don't have to read from start to end, you'll probably have to start reading from the beginning and continue reading until you find the string.
    The string will be a sequence of bytes, and you need to find that sequence of bytes. That means you will have to know how the string was encoded when written to the file, i.e. whether it is ASCII or some version of Unicode. Try finding a code example that searches a file for a pattern of bytes.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2018
    Posts
    16

    Re: How to find a particular string from a file written with binary writer

    Quote Originally Posted by passel View Post
    Well, unless you know where the string is you're going to have to search for it.
    And while you don't have to read from start to end, you'll probably have to start reading from the beginning and continue reading until you find the string.
    The string will be a sequence of bytes, and you need to find that sequence of bytes. That means you will have to know how the string was encoded when written to the file, i.e. whether it is ASCII or some version of Unicode. Try finding a code example that searches a file for a pattern of bytes.
    Thank You, for your valuable suggestion. Now can you please provide me a link or an example code from which i can understand it better...

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,583

    Re: How to find a particular string from a file written with binary writer

    Well, I can't tell from here how your strings are stored in your file, that is something you will have to determine.
    When I mentioned "Try finding a code example", I meant for you to search. For me to provide a link or an example I would have to search so the work would be the same, it is just a matter of who is doing it.

    There are also many possible approaches to take, so I don't want to take the time to figure out which approach I think might be good for me in a given situation, and it not be as good in yours.

    But in the interest of a bit of poking around, here is a link about reading from Binary files. Since you don't want to read the whole file, I assume you wouldn't use the ReadAllBytes method, but rather do a series of Read method calls of a filestream object of the file.

    Then at the end of this link, the vb.net code posted by Shabeer Shabeer seems like a reasonable byte array search for a string (assuming UTF8 string encoding and looking for a case sensitive match). It isn't as good as Knuth-Morris-Pratt algorithm, but is simple and, as I said, a reasonable search algorithm. Of course the earlier example at that link of using the System.Array.IndexOf method probably has a fairly efficient search algorithm behind it so may be faster.

    These are all things you should test, as it is your best interest at this point and time. It may be in my best interest at some time in the future, but isn't at the moment.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jan 2018
    Posts
    16

    Re: How to find a particular string from a file written with binary writer

    Thank you, Passel i will follow your advise and try to do it in this way..

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width