Writing files using O_DIRECT in C

I am trying to write .pgm images using the O_DIRECT flag in open().
I have a char* buffer which has the image data.

I know that I have to align the buffers and have done that using posix_memalign() yet only a part of the image gets written.

Has someone used O_DIRECT for writing files successfully? Can you help me on how to proceed with this?

Thanks

Can you show us you code?

Here is the function. It does not write the entire image.

// imgdata is the image data to be written into a pgm image file

int writePgm( char* Filename, unsigned char* imgdata, int width, int height )
{
   void *buf;
   int ret=0;

   unsigned long long int bytes_written=0;
   int ps=getpagesize();
   posix_memalign(&buf, ps, height*width*2);  //each pixel is equvivalent to 2 bytes
   memcpy(buf ,imgdata, height*width*2);

   FILE* stream;
   int fd = open(Filename, O_CREAT | O_WRONLY |O_DIRECT, 00666);
   stream = fdopen(fd, "wb");

   if( stream == NULL)
   {
     perror( "Can't open image file" );
     return 1;
   }
   
   fprintf( stream, "P5\n%u %u 255\n", width, height );
   
   fwrite( buf, ps, width*height/ps, stream );
   
   fclose( stream );
   close(fd);
   return 0;
}

When using O_DIRECT on Linux, every block written needs to be properly aligned and the correct size, including the very last one, which is a real difficulty when creating files that don't perfectly match that size.

You're probably having problems with the last block not getting written, and also because you're using fwrite(), which does buffered output. But the buffer is not likely to be properly aligned and could also be smaller than a page size.