Read then Write...

Discussion in 'Programming & Scripting' started by tOxiC x, Feb 18, 2009 with 4 replies and 217 views.

  1. tOxiC x

    tOxiC x Member

    Messages:
    656
    Ratings:
    116
    Ok, I am stuck in a problem here. I know how to read a string from a file and then write it as a string. I can't do it as bytes for some reason. What I want it to do it go to and offset (r.BaseStream.Position = 0xAC00:wink: then read the next 50 bytes and then these bytes will be a variable in my lower portion of code a few lines down where it will write these bytes (basically like copy and paste) to a different offset of null bytes. (overwritting these null bytes with the variable from the binaryreader).

    If you get what I am saying, please help out. My code I was trying to do, was this:


    Code:
    BinaryReader r = new BinaryReader(new FileStream(textBox1.Text, FileMode.Open));
    r.BaseStream.Position = 0xAC00;
    string data = r.ReadBytes(0x50).ToString();
    r.Close();
    BinaryWriter b = new BinaryWriter(new FileStream(textBox1.Text, FileMode.Open));
    b.Write("Test File");
    b.BaseStream.Position = 0xD000;
    b.Write(data);
    
    using textBox1.Text as my location.

    As you can see that code is flawed, it won't work the way I need it to. Also, everything is going in the same class so all the variables can be declared internally. Thanks for the help.
     
  2. GoldBl4d3

    GoldBl4d3 Retired Retired

    Messages:
    3,758
    Ratings:
    1,328
    almost had it, but 0x50 is an address, 0x establishes that it will be an address. just remove the 0x and you have 50 left over.
    also dont try to convert 50 bytes into a string. It doesnt work that way.(it does but not what your looking for)

    Your edited code:
    Code:
    BinaryReader r = new BinaryReader(new FileStream(textBox1.Text, FileMode.Open));
    r.BaseStream.Position = 0xAC00;
    string data = r.ReadBytes(50).ToString();
    r.Close();
    BinaryWriter b = new BinaryWriter(new FileStream(textBox1.Text, FileMode.Open));
    b.Write("Test File");
    b.BaseStream.Position = 0xD000;
    b.Write(data);
    
    using textBox1.Text as my location.
     
    • Like Like x 1
  3. OP
    tOxiC x

    tOxiC x Member

    Messages:
    656
    Ratings:
    116
    Ok, I fixed the address mistake, but how can I fix not converting it to string. I am very good with strings now after studying some sources just no bytes. If i use the:

    string data = r.ReadBytes(50);

    Then it is not a string. What would I use instead of string?

    byte[] data = r.ReadBytes(50); *solved*
    ----------

    Thanks, problem solved.
     
  4. superaison

    superaison Banned

    Messages:
    1,404
    Ratings:
    269
    0x means the int inputted is HEX.


    so 0x100 is 256 in decimal...

    0x means its using hex, and not decimal.

    Idk what your talking about "address" You mean, offset?

    thats what the


    r.BaseStream.Position = 0xAC00;

    thing is...
     
  5. OP
    tOxiC x

    tOxiC x Member

    Messages:
    656
    Ratings:
    116
    Blah I got it now. Yea it doesn't have to be an address really, of I do:

    b.BaseStream.Position = 0xAC00; <--- Address

    b.BaseStream.WriteByte(0x1A); <--- Byte value...

    I figured it all out and finished my app, thanks for the support. The only thing left is to add some touches :smile:.