How to write Binary data to a file???? [modified]
-
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
-
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
-
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?
-
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?
;) 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! }
-
;) 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! }
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.
-
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?
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++ -
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.
;) 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.
-
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?
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
-
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++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
-
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
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++ -
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++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