1

I want to write data into the file in binary form.

I was trying using the mentioned below

FILE *fp = fopen("binaryoutput.rgb888", "ab+");

for(int m=0; m<height; m++)
{
   for (int n=0; n< width; n++)        
   {                            
    temp = (pOutputImg+m*3+n*3); // here pOutputImg & temp is a pointer to a unsigned char  
    fprintf(fp,"%u",*temp);             
   }        
}
fclose(fp);

I am able to get data which is strored at pOutputImg but not in binary form.

Can anyone guide me the correct step..

Thanks in advance

2
  • It is very difficult to figure out the intent. What are the types of temp and pOutputImg? Why do you multiply both m and n by 3? Shouldn't one of them be multiplied by the other's dimension? Commented Mar 28, 2010 at 7:43
  • @Marcelo Cantos : That is for obtaining the data in correct format for RGB888.. by writing that file in binary format will give me the correct format for RGB888 image.. Commented Mar 28, 2010 at 7:46

2 Answers 2

7

Replace fprintf() with fwrite().

Ex:

fwrite(temp, sizeof(*temp), 1, fp);

The whole purpose of fprintf() is to format binary data as readable ascii ... the exact opposite of what you want. fwrite() is for directly writing binary data.

Sign up to request clarification or add additional context in comments.

Comments

2

If this is a pixmap of rgb triplets, you can write the binary data with one line:

fwrite(pOutputImg, 3, height * width, fp);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.