open file in c / create if file doesn't exist

Discussion in 'C Programming' started by norm4h8@yahoo.com, Apr 6, 2006.

  1. Guest

    Hi everyone!

    I have a problem with trying to open a file in C.

    The following line in my code is suppoed to open a specified file if it
    exists and create a new one with this name if one doesn't exist:

    if ((shfd = open(argv[2], O_CREAT | O_RDWR, 0644)) < 0)
    my_error("open failed");

    If I try to open a file that doesn't exist I get an error:
    Bus error

    And when it creates a file it sets permissions to -rw------ instead of
    -rw-r--r-- as I would hope... Is there something I'm not getting about
    how O_CREATE works?...

    However, if I try to open existing file that is not empty, it works
    fine...

    Course anyone please help me?

    thank you
     
    , Apr 6, 2006
    #1
    1. Advertising

  2. Guest

    another important thing - it given an error because I map a portion of
    the file in memory, as in

    if ((mem = mmap(0,
    SIZE,
    PROT_READ | PROT_WRITE,
    MAP_SHARED,
    shfd,
    0)) == (void *)-1)
    my_error("mmap failed");


    and then I try to write something into it:

    sprintf(mem, "%d", getpid());

    The file remains empty and I get the "bus error" message.
    any help would be greatly appreciated...

    Thanks for your time
     
    , Apr 6, 2006
    #2
    1. Advertising

  3. tedu Guest

    wrote:
    > I have a problem with trying to open a file in C.
    >
    > The following line in my code is suppoed to open a specified file if it
    > exists and create a new one with this name if one doesn't exist:
    >
    > if ((shfd = open(argv[2], O_CREAT | O_RDWR, 0644)) < 0)
    > my_error("open failed");
    >
    > If I try to open a file that doesn't exist I get an error:
    > Bus error
    >
    > And when it creates a file it sets permissions to -rw------ instead of
    > -rw-r--r-- as I would hope... Is there something I'm not getting about
    > how O_CREATE works?...


    you're going to want to take this up in comp.unix.programmer. the
    answer you're probably going to get will include the words umask and
    ftruncate.
     
    tedu, Apr 6, 2006
    #3
  4. #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <errno.h>

    shfd = open(argv[2],
    O_RDWR | O_CREAT,
    S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
    if (shfd < 0) {
    perror("open");
    // exit code here
    }
    // proceed code here
     
    Chandra Kalle, Apr 13, 2006
    #4
  5. int rv;
    // open fd (last post)

    rv = lseek(shfd, SIZE+1, SEEK_SET);
    if (rv < 0) {
    // handle error
    }

    rv = write(shfd, "", 1);
    if (rv < 0) {
    // handle error
    }

    rv = lseek(shfd, 0, SEEK_SET);
    // handle error
    }

    mem = mmap(0, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shfd, 0);
    if (mem == MAP_FAILED) {
    // handle error
    }
     
    Chandra Kalle, Apr 13, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Y.S.
    Replies:
    3
    Views:
    1,087
    strajan
    Sep 17, 2003
  2. LT
    Replies:
    7
    Views:
    2,151
    Phlip
    Jul 25, 2004
  3. Markus Mohr
    Replies:
    7
    Views:
    276
    Thomas 'PointedEars' Lahn
    Nov 28, 2003
  4. Peng Yu
    Replies:
    1
    Views:
    93
    J├╝rgen Exner
    Nov 30, 2008
  5. Avnesh Shakya
    Replies:
    3
    Views:
    136
    Jens Thoms Toerring
    May 27, 2013
Loading...

Share This Page