Thread: Help: open(file,flag, acc) returns 0 and write to the screen, insted of the file ???

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    4

    Angry Help: open(file,flag, acc) returns 0 and write to the screen, insted of the file ???

    I am out of idea what to do to resolve the problem!

    I need to use the open(file, for.., access) function to write a file.

    Never have the situation like that: it is return 0 - zero.
    As a result all write(..) going to the screen!

    What the problem it could be?

    I do not even know what to show in code.
    Opened this way:

    Code:
          if (fdRPT  = open( rpt_fl,  O_WRONLY|O_CREAT|O_TRUNC, 0666) == -1 )
          {  .../*error*/  ;}
    The 'rpt_fl' is fine
    Code:
    ...
           printf ("\n -- The report file is: >%s< -- \n",rpt_fl);
    ....
    prints out:
    Code:
     -- The report file is: >DATA/elm0653M.rpt.0008< --
    and
    Code:
    > ll DATA/elm0653M.rpt.0008
    -rw-rw-rw-   1 dca0701  dstnse         0 Mar  8 13:06 DATA/elm0653M.rpt.0008
    I undestand that the system by default should have the '0' descriptor associated with the standart output and have it unavailable (or already assighned) on any, even first a file descriptor request (by the open(..),) but it is not happening in my case.
    I am stucked with how get it resolve.

    What could be the reason for such behavior?
    What would you advise to do to realise the problem or correct it?

    I am appreciate any help and every try!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > if (fdRPT = open( rpt_fl, O_WRONLY|O_CREAT|O_TRUNC, 0666) == -1 )
    Precedence!

    You wrote this.
    if (fdRPT = (open( rpt_fl, O_WRONLY|O_CREAT|O_TRUNC, 0666) == -1) )


    You wanted this
    if ( (fdRPT = open( rpt_fl, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1 )
    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.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Code:
          if (fdRPT  = open( rpt_fl,  O_WRONLY|O_CREAT|O_TRUNC, 0666) == -1 )
    I think this is assigning the boolean value of
    Code:
    open( rpt_fl,  O_WRONLY|O_CREAT|O_TRUNC, 0666) == -1
    to "fdRPT". "open" is probably working fine, and returning something besides "-1", say i. Therefore "i == -1" evaluates to false, or, say, 0. So your assigning 0 to "fdRPT" which is the FD for STDOUT. You probably want to force the order of the operations to
    Code:
          if ((fdRPT  = open( rpt_fl,  O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1 )

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    4
    Quote Originally Posted by Salem View Post
    > ....
    Precedence!
    ...
    Quote Originally Posted by nadroj View Post
    > ....
    I think this is assigning the boolean value of
    ...
    It is exactly what I have!
    Thank you very much!!

    Now it works as it should!!!

Popular pages Recent additions subscribe to a feed