Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. write bytes to a file

write bytes to a file

Scheduled Pinned Locked Moved C#
helpperformancequestion
15 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Le centriste

    flush the BinaryWriter before re-reading the data. MemoryStream mem = new MemoryStream(); BinaryWriter bin = new BinaryWriter(mem); bin.Write(unicodebytes, 0, number); bin.Write(unicodebytes1, 0, number1); **bin.Flush();** // <<<----------------

    ----- If atheism is a religion, then not collecting stamps is a hobby. -- Unknown

    W Offline
    W Offline
    WhiteGirl23
    wrote on last edited by
    #6

    thank for your help but still doesn't work

    1 Reply Last reply
    0
    • L Luc Pattyn

      try to explain the context : what are you trying to achieve ? is it binary data ? text ? in ASCII ? in Unicode ? are they both Word documents ? where does the data originate ? show the declarations ! and specify the symptoms in detail: what does "can see" mean ? is the first file OK, or just recognizable ? and what about the second: nothing at all, some garbage, some parts recognizable, what ? if you are not explicit about these, you either will get no answers, or some answer that is as mysterious as your questions are... :)

      Luc Pattyn


      try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


      W Offline
      W Offline
      WhiteGirl23
      wrote on last edited by
      #7

      the first file is seen very good. My files are document files. But the second file is missing. I turned my documents files in bytes and these files are written to the memory stream. From the memory stream (where I have the 2 documents in bytes)I want to see the 2 files in one file. Thanks.

      L 1 Reply Last reply
      0
      • W WhiteGirl23

        my code is: //write in memory the files MemoryStream mem = new MemoryStream(); BinaryWriter bin = new BinaryWriter(mem); bin.Write(unicodebytes, 0, number); bin.Write(unicodebytes1, 0, number1); //unicodebytes,unicodebytes1 are vectors where I keep the bytes from the files //read the files BinaryReader binread = new BinaryReader(mem); mem.Position = 0; byte[] byteArray = new byte[mem.Length]; int count; count = mem.Read(byteArray, 0,1); while (count < mem.Length) { byteArray[count++] = Convert.ToByte(mem.ReadByte()); File.WriteAllBytes("C:\\text1.doc", byteArray); My problem is that I can see the first file in Word but I can't see the second file.

        L Offline
        L Offline
        Le centriste
        wrote on last edited by
        #8

        There are a couple of things strange in your code. 1- You assign a BinaryReader but you do not use it. 2- You are writing a bunch of bytes to a MemoryStream. Are you just trying to copy the 2 byte arrays to a single file (if not, what your code is doing totally escapes me)? This is not really needed. Try this instead:

        FileStream fs = File.Create(@"C:\text.doc");
        fs.Write(unicodebytes, 0, unicodebytes.Length);
        fs.Write(unicodebytes1, 0, unicodebytes1.Length);
        fs.Flush();
        fs.Close();

        If this is not the case, please explain what you are trying to do.

        ----- If atheism is a religion, then not collecting stamps is a hobby. -- Unknown

        W 1 Reply Last reply
        0
        • L Le centriste

          There are a couple of things strange in your code. 1- You assign a BinaryReader but you do not use it. 2- You are writing a bunch of bytes to a MemoryStream. Are you just trying to copy the 2 byte arrays to a single file (if not, what your code is doing totally escapes me)? This is not really needed. Try this instead:

          FileStream fs = File.Create(@"C:\text.doc");
          fs.Write(unicodebytes, 0, unicodebytes.Length);
          fs.Write(unicodebytes1, 0, unicodebytes1.Length);
          fs.Flush();
          fs.Close();

          If this is not the case, please explain what you are trying to do.

          ----- If atheism is a religion, then not collecting stamps is a hobby. -- Unknown

          W Offline
          W Offline
          WhiteGirl23
          wrote on last edited by
          #9

          thanks for your help but I have the same problem. I can see the first file but I can't see the second. I want to copy 2 documents files in one file.

          L 1 Reply Last reply
          0
          • W WhiteGirl23

            the first file is seen very good. My files are document files. But the second file is missing. I turned my documents files in bytes and these files are written to the memory stream. From the memory stream (where I have the 2 documents in bytes)I want to see the 2 files in one file. Thanks.

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #10

            Now who claims that appending two document files would result in one document file with all the document content ? You can use a hex dump utility (Visual Studio !), or maybe Wordpad, to check your concatenation succeeded technically, Word has already proven it did not succeed functionaly. You can concatenate two text files, two C# source files (that will result in some compile errors tho), maybe two mp3 files. But you cant get the result you're after when you concatenate two JPEG files, two Visual Studio project files, two MS Word files, etc. You need something smarter and more difficult to achieve that. :)

            Luc Pattyn


            try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


            1 Reply Last reply
            0
            • W WhiteGirl23

              thanks for your help but I have the same problem. I can see the first file but I can't see the second. I want to copy 2 documents files in one file.

              L Offline
              L Offline
              Le centriste
              wrote on last edited by
              #11

              Did you try to run it through a debugger? is it possible that unicodebytes1 is empty? Make sure your 2 byte arrays actually contain the bytes.

              ----- If atheism is a religion, then not collecting stamps is a hobby. -- Unknown

              W 1 Reply Last reply
              0
              • W WhiteGirl23

                my code is: //write in memory the files MemoryStream mem = new MemoryStream(); BinaryWriter bin = new BinaryWriter(mem); bin.Write(unicodebytes, 0, number); bin.Write(unicodebytes1, 0, number1); //unicodebytes,unicodebytes1 are vectors where I keep the bytes from the files //read the files BinaryReader binread = new BinaryReader(mem); mem.Position = 0; byte[] byteArray = new byte[mem.Length]; int count; count = mem.Read(byteArray, 0,1); while (count < mem.Length) { byteArray[count++] = Convert.ToByte(mem.ReadByte()); File.WriteAllBytes("C:\\text1.doc", byteArray); My problem is that I can see the first file in Word but I can't see the second file.

                O Offline
                O Offline
                originSH
                wrote on last edited by
                #12

                If your trying to combine 2 wrod doc's it's not that simple. Word docs use Structured Storage[^] (aka DocFile[^]) for pre 2007 files. For 2007 there is a new XML format ... you can do your own google for that :P Either way you cannot simply concatenate these documents.

                W 1 Reply Last reply
                0
                • L Le centriste

                  Did you try to run it through a debugger? is it possible that unicodebytes1 is empty? Make sure your 2 byte arrays actually contain the bytes.

                  ----- If atheism is a religion, then not collecting stamps is a hobby. -- Unknown

                  W Offline
                  W Offline
                  WhiteGirl23
                  wrote on last edited by
                  #13

                  thanks for your help. The size of my new created file is equal with the size of the first file + the size of the second file. The unicodebytes1 is not null. thanks again.

                  O 1 Reply Last reply
                  0
                  • W WhiteGirl23

                    thanks for your help. The size of my new created file is equal with the size of the first file + the size of the second file. The unicodebytes1 is not null. thanks again.

                    O Offline
                    O Offline
                    originSH
                    wrote on last edited by
                    #14

                    Combining 2 word documents by appending one to the other will never work. Your barking up the wrong tree.

                    1 Reply Last reply
                    0
                    • O originSH

                      If your trying to combine 2 wrod doc's it's not that simple. Word docs use Structured Storage[^] (aka DocFile[^]) for pre 2007 files. For 2007 there is a new XML format ... you can do your own google for that :P Either way you cannot simply concatenate these documents.

                      W Offline
                      W Offline
                      WhiteGirl23
                      wrote on last edited by
                      #15

                      thanks for all your help. I found how to resolve my problem(with tx textcontrol). Thanks again. Regards, WhiteGirl23

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups