LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-28-2010, 04:02 PM   #1
anchit87
LQ Newbie
 
Registered: Feb 2010
Posts: 1

Rep: Reputation: 0
Writing files using O_DIRECT in C


Hi,

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
 
Old 02-28-2010, 08:51 PM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Quote:
Has someone used O_DIRECT for writing files successfully?
I haven't, because there isn't a good reason to.

However, the following code illustrates its use. Note that both the buffer start and end should be block aligned.
Code:
#define _GNU_SOURCE
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#define BLOCKSIZE 512
char image[] =
{
	'P', '5', ' ', '2', '4', ' ', '7', ' ', '1', '5', '\n',
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 3, 3, 3, 3, 0, 0, 7, 7, 7, 7, 0, 0,11,11,11,11, 0, 0,15,15,15,15, 0,
	0, 3, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0,11, 0, 0, 0, 0, 0,15, 0, 0,15, 0,
	0, 3, 3, 3, 0, 0, 0, 7, 7, 7, 0, 0, 0,11,11,11, 0, 0, 0,15,15,15,15, 0,
	0, 3, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0,11, 0, 0, 0, 0, 0,15, 0, 0, 0, 0,
	0, 3, 0, 0, 0, 0, 0, 7, 7, 7, 7, 0, 0,11,11,11,11, 0, 0,15, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0,
};
int main()
{
	void *buffer;
	posix_memalign(&buffer, BLOCKSIZE, BLOCKSIZE);
	memcpy(buffer, image, sizeof(image));
	int f = open("feep.pgm", O_CREAT|O_TRUNC|O_WRONLY|O_DIRECT, S_IRWXU);
	write(f, buffer, BLOCKSIZE);
	close(f);
	free(buffer);
	return 0;
}

Last edited by neonsignal; 02-28-2010 at 08:55 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to use O_DIRECT ? Stephan_Craft Linux - Server 1 10-05-2009 05:16 AM
file writes to RAID using posix_fadvise vs O_DIRECT zobtempo Linux - Software 0 05-02-2007 10:11 AM
LXer: Linux: Accessing Files With O_DIRECT LXer Syndicated Linux News 0 01-11-2007 07:03 PM
gcc question....on solaris....O_DIRECT Petros Koutoupis Solaris / OpenSolaris 2 09-14-2006 01:53 PM
Max read size for O_DIRECT file I/O bigqueso Linux - Software 1 06-02-2006 10:47 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration