Find the answer to your Linux question:
Results 1 to 3 of 3
i tried my hand at low level I/O commnads... I have problem with 'open' command . .I wrote the following code . #include <fcntl.h> #include <stdio.h> #include <sys/stat.h> #include <sys/types.h> ...
  1. #1
    Just Joined!
    Join Date
    Jun 2010
    Posts
    2

    problem with 'open' call . .

    i tried my hand at low level I/O commnads...

    I have problem with 'open' command . .I wrote the following code .

    #include <fcntl.h>
    #include <stdio.h>
    #include <sys/stat.h>
    #include <sys/types.h>
    #include <unistd.h>
    int main (int argc, char* argv[])
    {
    /* The path at which to create the new file. */
    char* path = argv[1];
    /* The permissions for the new file. */
    mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH;
    /* Create the file. */
    int fd = open (path, O_WRONLY | O_EXCL | O_CREAT, mode);
    if (fd == -1) {
    /* An error occurred. Print an error message and bail. */
    perror (“open”);
    return 1;
    }
    return 0;
    }

    this code works properly . .. but when i change the path to "/etc" or "/home/abhishek/Documents" or any other path . .i get 'File exits' or 'Permission denied' . .error messages. . .i tried to execute code logging in as root also .. .but same messages appear . . .please tell me what is the correct syntax(if it is a syntactical error) . .to specify desired path where file is to be created . . .
    abhishek_mathur is offline

  2. #2
    Linux Engineer hazel's Avatar
    Join Date
    May 2004
    Location
    Harrow, UK
    Posts
    955
    Well, I tried this and it worked for me whether I wrote the pathname into the code or made it an argument. But once you have run it, that file exists, so if you run the program again without removing the file between runs, you get a "file exists" error.

    btw, you need to give the pathname to perror, not the name of the "open" function. perror expects a char * argument.

    Did you include the actual filename as part of your path? Or only directories? The latter does not work.
    "I'm just a little old lady; don't try to dazzle me with jargon!"

  3. #3
    Just Joined!
    Join Date
    Jun 2010
    Posts
    2

    thank you . .

    thanks a lot . . . i was not putting the filename while giving the path in code . . .i just mentioned the directories . . .actually i didn't know the concept of argv and argc . . .. i thought argv always points to the present working directory . . i didn't know that i can point it to any directory while executing the programme . . i always coded with . .. .
    void main(void) . . . .again thanks a lot . .

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...