help w/ write() function and open() function

This is a discussion on help w/ write() function and open() function within the C Programming forums, part of the General Programming Boards category; I'm having some trouble w/ the write() function and open() function. here we go BTW -> fd = file descriptor ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    9

    help w/ write() function and open() function

    I'm having some trouble w/ the write() function and open() function.

    here we go
    BTW -> fd = file descriptor

    Code:
    int n_open_write(const char *pathname,unsigned int buffsize)
    {
    	int fd;
    
    	fd = open(pathname,O_WRONLY);
    
    	if(fd == -1)
    	{
    	n_close(fd);
    	fd = open(pathname,O_CREAT);
    
    	}
    
           return fd;
    }


    w/ this function my question is....if fd does equal -1, will i have to close that fd(like i'm doing above) to reopen fd w/ O_CREAT.

    and secondly....when i implement this it tells me that my file that i've created is not readable, which i'm guessin is because i'm not passing in a 3rd argument to open when i use O_CREAT, so what would i pass into it if i wanted to creat a file w/ write permissions.


    and here is my last question.

    Code:
    int n_putc(int c, int fd)
    {
    int status;
    unsigned char *mychar;
    *mychar = (unsigned char)c;
    
    status = write(fd,mychar,1);
    
    if(status == -1)
    	return -1;
    else
    	return 1;
    }
    when i use this, i seg fault obviously. my question is.....what type does mychar have to be, I've got conflicting man pages telling me that it takes a const void* and another that says it just takes a char*.


    thanks ahead of time to anyone who posts.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > if fd does equal -1, will i have to close that fd
    No, you didn't open anything, so there is nothing to close.

    > i'm not passing in a 3rd argument to open when i use O_CREAT
    Big mistake - it will get created with random permissions in that case.
    Learn octal!
    Code:
    rwxrwxrwx
    421421421
    Each rwx maps to a single octal digit, so a permission of 640 would be
    rw for user
    r for group
    - for everyone else.
    But also see the description in
    man 2 umask
    for details of how the permissions are modified.

    > unsigned char *mychar;
    > *mychar = (unsigned char)c;
    > status = write(fd,mychar,1);
    When something expects a pointer, it is not sufficient to simply declare a variable of that type.
    More often than not, it needs to point somewhere.

    unsigned char mychar = (unsigned char)c;
    status = write(fd,&mychar,1);
    Use & to create a pointer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21