Complex statement

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • seema_coma@yahoo.co.in

    #1

    Complex statement

    Hi all,

    Can somebody explain what this statement does,
    if ((tempFD = open(tempFileNa me, O_RDWR | O_CREAT | O_EXCL)) < 0)

    Thanks in advance
    Seema

  • manoj1978@gmail.com

    #2
    Re: Complex statement


    seema_coma@yaho o.co.in wrote:[color=blue]
    > Hi all,
    >
    > Can somebody explain what this statement does,
    > if ((tempFD = open(tempFileNa me, O_RDWR | O_CREAT | O_EXCL)) < 0)
    >
    > Thanks in advance
    > Seema[/color]

    tempFD = open(tempFileNa me, O_RDWR | O_CREAT | O_EXCL);
    if (tempFD < 0)

    Do "man open" to see open's input parameters and return value.

    Comment

    • Martin Ambuhl

      #3
      Re: Complex statement

      seema_coma@yaho o.co.in wrote:[color=blue]
      > Hi all,
      >
      > Can somebody explain what this statement does,
      > if ((tempFD = open(tempFileNa me, O_RDWR | O_CREAT | O_EXCL)) < 0)[/color]

      If we knew for sure what version of what library you were using, some
      might. But they shouldn't, since the experts for your platform or
      implementation have newsgroups of their own. Unless your question is
      about C as defined by the standard, no reliable substantive answers from
      comp.lang.c should be expected. In your case, the function open() is
      not defined as part of the standard C libaries (nor are O_RDWR, O_CREAT,
      or O_EXCL). Questions about fopen(), a standard C function, we can
      answer. It is most likely (although definitely not certain) that one of
      the unix programming newsgroups is what you want.

      Comment

      • John Bode

        #4
        Re: Complex statement


        seema_coma@yaho o.co.in wrote:[color=blue]
        > Hi all,
        >
        > Can somebody explain what this statement does,
        > if ((tempFD = open(tempFileNa me, O_RDWR | O_CREAT | O_EXCL)) < 0)
        >
        > Thanks in advance
        > Seema[/color]

        The open() function itself is not part of the standard C library, but
        part of the Unix (POSIX?) API. Refer to the man page for specifics.
        The above is a shorthand equivalent to the following:

        char *tempFileName = ...; /* some file name */
        int fileAccessMask = 0_RDWR | O_CREAT | O_EXCL;
        int tempFD = open (tempFileName, fileAccessMask) ;

        if (tempFD < 0)
        {
        /* do something */
        }

        Essentially, the open() call and the test are being squashed into the
        conditional expression.

        Comment

        • tedu

          #5
          Re: Complex statement

          John Bode wrote:[color=blue]
          > seema_coma@yaho o.co.in wrote:[color=green]
          > > Hi all,
          > >
          > > Can somebody explain what this statement does,
          > > if ((tempFD = open(tempFileNa me, O_RDWR | O_CREAT | O_EXCL)) < 0)
          > >
          > > Thanks in advance
          > > Seema[/color]
          >
          > The open() function itself is not part of the standard C library, but
          > part of the Unix (POSIX?) API. Refer to the man page for specifics.
          > The above is a shorthand equivalent to the following:
          >
          > char *tempFileName = ...; /* some file name */
          > int fileAccessMask = 0_RDWR | O_CREAT | O_EXCL;
          > int tempFD = open (tempFileName, fileAccessMask) ;[/color]

          OT, but you should never call open with O_CREAT and no mode.

          Comment

          • Anonymous 7843

            #6
            Re: Complex statement

            In article <1125660780.124 997.93420@z14g2 000cwz.googlegr oups.com>,
            John Bode <john_bode@my-deja.com> wrote:[color=blue]
            >
            > char *tempFileName = ...; /* some file name */
            > int fileAccessMask = 0_RDWR | O_CREAT | O_EXCL;[/color]

            gcc says: invalid suffix "_RDWR" on integer constant

            Comment

            • kernelxu@hotmail.com

              #7
              Re: Complex statement

              seema_c...@yaho o.co.in wrote:[color=blue]
              > Can somebody explain what this statement does,
              >if ((tempFD = open(tempFileNa me, O_RDWR | O_CREAT | O_EXCL)) < 0)[/color]

              open() is not a standard C function, it belongs to UNIX.
              if calling open() failed, it will return -1.
              therfore, the expression " (tempFD = open(tempFileNa me, O_RDWR |
              O_CREAT | O_EXCL)) < 0" is to check whether the call is successful or
              not.

              Comment

              Working...