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 / C++ / MFC
  4. How to write Binary data to a file???? [modified]

How to write Binary data to a file???? [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialhelpquestion
11 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.
  • T Offline
    T Offline
    TooShy2Talk
    wrote on last edited by
    #1

    Hello guys, Hope you can help me with this one. For example, BYTE buf[100000]; // contains binary data of an image I wanted it to be written in an image file(*.bmp). Thanks in advance.

    modified on Wednesday, April 22, 2009 10:28 PM

    F 1 Reply Last reply
    0
    • T TooShy2Talk

      Hello guys, Hope you can help me with this one. For example, BYTE buf[100000]; // contains binary data of an image I wanted it to be written in an image file(*.bmp). Thanks in advance.

      modified on Wednesday, April 22, 2009 10:28 PM

      F Offline
      F Offline
      Fiona1011
      wrote on last edited by
      #2

      ;) You can use CFile object to write or read a binary file.

      T 1 Reply Last reply
      0
      • F Fiona1011

        ;) You can use CFile object to write or read a binary file.

        T Offline
        T Offline
        TooShy2Talk
        wrote on last edited by
        #3

        Thanks for the comment. Well I have tried using CStdioFile and had successfuly created a file but the file cannot be opened to any image viewer. Error message implied of the wrong header. // my code here CStdioFile fp; CFileException e; if(fp.Open("c:\\Image.bmp",CFile::modeCreate|CFile::modeWrite|CFile::typeBinary,&e)) fp.Write(&buf,100000); // end * file was created and not 0 byte. Did I use it wright? or There's something wrong with my code?

        F C D 3 Replies Last reply
        0
        • T TooShy2Talk

          Thanks for the comment. Well I have tried using CStdioFile and had successfuly created a file but the file cannot be opened to any image viewer. Error message implied of the wrong header. // my code here CStdioFile fp; CFileException e; if(fp.Open("c:\\Image.bmp",CFile::modeCreate|CFile::modeWrite|CFile::typeBinary,&e)) fp.Write(&buf,100000); // end * file was created and not 0 byte. Did I use it wright? or There's something wrong with my code?

          F Offline
          F Offline
          Fiona1011
          wrote on last edited by
          #4

          ;) You can write as following: CStdioFile fp_read,fp_write; CFileException e; char buf[1024]; fp_read.Open("c:\\ImageSrc.bmp",CFile::modeRead|CFile::typeBinary,&e); fp_write.Open("c:\\Image.bmp",CFile::modeCreate|CFile::modeWrite|CFile::typeBinary,&e); while (int count = fp_read.Read(buf,sizeof(buf))) { fp_write.Write(buf,count); fp_write.Seek(0,CFile::end); // Important! You lose it! fp_read.Seek(0,CFile::current); // Important! You lose it! }

          T 1 Reply Last reply
          0
          • F Fiona1011

            ;) You can write as following: CStdioFile fp_read,fp_write; CFileException e; char buf[1024]; fp_read.Open("c:\\ImageSrc.bmp",CFile::modeRead|CFile::typeBinary,&e); fp_write.Open("c:\\Image.bmp",CFile::modeCreate|CFile::modeWrite|CFile::typeBinary,&e); while (int count = fp_read.Read(buf,sizeof(buf))) { fp_write.Write(buf,count); fp_write.Seek(0,CFile::end); // Important! You lose it! fp_read.Seek(0,CFile::current); // Important! You lose it! }

            T Offline
            T Offline
            TooShy2Talk
            wrote on last edited by
            #5

            I have tried your code but has an error accessing Image.bmp. Another thing is that I will not copy from a bitmap file to another bitmap file. Instead, I have already a binary data in an array to copy to a bitmap file. Hope you can help me with this. Thanks.

            F 1 Reply Last reply
            0
            • T TooShy2Talk

              Thanks for the comment. Well I have tried using CStdioFile and had successfuly created a file but the file cannot be opened to any image viewer. Error message implied of the wrong header. // my code here CStdioFile fp; CFileException e; if(fp.Open("c:\\Image.bmp",CFile::modeCreate|CFile::modeWrite|CFile::typeBinary,&e)) fp.Write(&buf,100000); // end * file was created and not 0 byte. Did I use it wright? or There's something wrong with my code?

              C Offline
              C Offline
              Cedric Moonen
              wrote on last edited by
              #6

              Well, of course if you want to have a valid bmp image in the file, you should follow the .bmp file format. You can't just write a bunch of bytes into a file and expects that it will be interpreted as a bitmap. See here[^] for more information.

              Cédric Moonen Software developer
              Charting control [v2.0 - Updated] OpenGL game tutorial in C++

              D 1 Reply Last reply
              0
              • T TooShy2Talk

                I have tried your code but has an error accessing Image.bmp. Another thing is that I will not copy from a bitmap file to another bitmap file. Instead, I have already a binary data in an array to copy to a bitmap file. Hope you can help me with this. Thanks.

                F Offline
                F Offline
                Fiona1011
                wrote on last edited by
                #7

                ;) Your code:fp.Write(&buf,100000); The length of buf may be not enough. So you haven't copy the whole binary data. Are you sure there are all binary data in the array? If the answer is YES,you may use cycle to write in a bitmap file.Every time write 1024B binary data or more; My code which send to you have no error when it runs in my computer.You may send me your error information.

                1 Reply Last reply
                0
                • T TooShy2Talk

                  Thanks for the comment. Well I have tried using CStdioFile and had successfuly created a file but the file cannot be opened to any image viewer. Error message implied of the wrong header. // my code here CStdioFile fp; CFileException e; if(fp.Open("c:\\Image.bmp",CFile::modeCreate|CFile::modeWrite|CFile::typeBinary,&e)) fp.Write(&buf,100000); // end * file was created and not 0 byte. Did I use it wright? or There's something wrong with my code?

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #8

                  TooShy2Talk wrote:

                  Well I have tried using CStdioFile...

                  Why are you not using CFile?

                  "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                  "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                  1 Reply Last reply
                  0
                  • C Cedric Moonen

                    Well, of course if you want to have a valid bmp image in the file, you should follow the .bmp file format. You can't just write a bunch of bytes into a file and expects that it will be interpreted as a bitmap. See here[^] for more information.

                    Cédric Moonen Software developer
                    Charting control [v2.0 - Updated] OpenGL game tutorial in C++

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #9

                    Cedric Moonen wrote:

                    ...if you want to have a valid bmp image in the file, you should follow the .bmp file format. You can't just write a bunch of bytes into a file and expects that it will be interpreted as a bitmap.

                    His initial post indicated that buf "contains binary data of an image."

                    "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                    "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                    C 1 Reply Last reply
                    0
                    • D David Crow

                      Cedric Moonen wrote:

                      ...if you want to have a valid bmp image in the file, you should follow the .bmp file format. You can't just write a bunch of bytes into a file and expects that it will be interpreted as a bitmap.

                      His initial post indicated that buf "contains binary data of an image."

                      "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                      C Offline
                      C Offline
                      Cedric Moonen
                      wrote on last edited by
                      #10

                      For me, "binary data of an image" means the data part of the image (so, excluding the header). I think he has an array that he filled in some way with the values of the pixels of the image. Of course, this is just my opinion because he wasn't really clear.

                      Cédric Moonen Software developer
                      Charting control [v2.0 - Updated] OpenGL game tutorial in C++

                      D 1 Reply Last reply
                      0
                      • C Cedric Moonen

                        For me, "binary data of an image" means the data part of the image (so, excluding the header). I think he has an array that he filled in some way with the values of the pixels of the image. Of course, this is just my opinion because he wasn't really clear.

                        Cédric Moonen Software developer
                        Charting control [v2.0 - Updated] OpenGL game tutorial in C++

                        D Offline
                        D Offline
                        David Crow
                        wrote on last edited by
                        #11

                        Cedric Moonen wrote:

                        ...he wasn't really clear.

                        True. I just didn't want to assume too much one way or the other.

                        "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                        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