Thread: Need help with write function

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    45

    Need help with write function

    Hi,
    I wrote this small piece of code to test the effect of O_DIRECT flag. But for some strange reason the program fails at the write function line with invalid argument. I cant seem to inderstand why it is failing to write. Could anyone please suggest what I am doing wrong.

    I am passing a writable file to to at time of invocation.

    Thanks.

    Code:
    #define _GNU_SOURCE
    #include <stdio.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <errno.h>
    #include <string.h>
    
    void usage(char *command)
    {
    
    	fprintf(stderr,"usage: %s FILENAME\n", command);
    	exit(1);
    }
    
    int main(int argc, char *argv[])
    {
    
    	char *filename;
    	int fd;
    	char buf[15] = {0};
    
    	if(argc != 2) usage(argv[0]);
    
    	filename = argv[1];
    
    	fd = open(filename, O_CREAT | O_DIRECT | O_RDWR, 0666);
    
    	if (fd < 0) {
    
    		fprintf(stderr, "ERROR: failed to open `%s': %s \n",
                                 filename, strerror(errno));
                    exit(1);
    
    	}
    
    
    
    	/* invoke the write system call . Not actually writing anything */
    	if((write(fd, &buf[0], sizeof(buf[0]) * 15)) == -1) {
    
    		fprintf(stderr, "Error writing to file:%s \n", strerror(errno));
    		close(fd);
    		exit(1);
    	}
    
    	
    	close(fd);
    
    	return 0;
    
    }
    Last edited by livin; 08-08-2012 at 01:46 PM.

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    I'll assume you're using Linux due to the combination of O_DIRECT and _GNU_SOURCE.

    When you get an error like this, a good place to start is the man pages. I'll quote the relevant bit of the ERRORS section of write(2):
    EINVAL - fd is attached to an object which is unsuitable for writing; or the file was opened with the O_DIRECT flag, and either the address specified in buf, the value specified in count, or the current file offset is not suitably aligned.
    If you look at open(2):
    The O_DIRECT flag may impose alignment restrictions on the length and address of userspace buffers and the file offset of I/Os.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    45
    yeah I saw that..but I couldn't find anything abt how to use it right. Could you see some obvious solutions I can try?

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    The obvious solution is to make sure everything is properly aligned. Maybe you'd be interested in the posix_memalign() function.

    If you saw that there were alignment restrictions with O_DIRECT, why did you consider EINVAL to be "strange"?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 01-04-2012, 01:25 AM
  2. Function Write()
    By samuelmoneill in forum C Programming
    Replies: 3
    Last Post: 04-17-2009, 04:36 AM
  3. help w/ write() function and open() function
    By Landroid in forum C Programming
    Replies: 1
    Last Post: 04-10-2005, 11:38 AM
  4. How do I write this function?
    By Cyb3rT3k in forum C Programming
    Replies: 12
    Last Post: 05-02-2004, 04:23 PM
  5. help me to write this function ^^
    By samidang in forum C Programming
    Replies: 1
    Last Post: 06-06-2002, 01:07 PM