Complex statement

S

seema_coma

Hi all,

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

Thanks in advance
Seema
 
M

manoj1978

Hi all,

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

Thanks in advance
Seema

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

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

Martin Ambuhl

Hi all,

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

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.
 
J

John Bode

Hi all,

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

Thanks in advance
Seema

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.
 
T

tedu

John said:
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);

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

Anonymous 7843

char *tempFileName = ...; /* some file name */
int fileAccessMask = 0_RDWR | O_CREAT | O_EXCL;

gcc says: invalid suffix "_RDWR" on integer constant
 
K

kernelxu

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

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top