[Search for users] [Overall Top Noters] [List of all Conferences] [Download this site]

Conference turris::decc

Title:DECC
Notice:General DEC C discussions
Moderator:TLE::D_SMITHNTE
Created:Fri Nov 13 1992
Last Modified:Fri Jun 06 1997
Last Successful Update:Fri Jun 06 1997
Number of topics:2212
Total number of notes:11045

2145.0. "fscanf behaviour?" by COMICS::EDWARDSN (Dulce et decorum est pro PDP program) Wed Apr 09 1997 07:37

I have, I think tracked this down to a difference between VAXCRTL and DECC$SHR,
I'm not sure whether to call it a bug or a feature or a change in behaviour, but:

The following, compiled under VAX C running on VMS 6.2 runs as below:

$ R TESTPROG
Enter script file name > thing
Enter script file name > thing

However, compiled using DEC C running on VMS 6.2 it runs as thus:

$ R TESTPROG
Enter script file name > thing
thingEnter script file name > thing

i.e. the typed input is written back to the output.

I'm not sure what this counts as, but I'm hoping that someone may be
able to shed some light on how and why it's behaving thus.

Neil.



#include stdio
void main()
{
    FILE *screen;
    char fred[100];

    screen = fopen ( "SYS$COMMAND", "r+" );

    fprintf ( screen, "Enter script file name > " );
    fscanf ( screen, "%s", fred );

/*    fflush( screen );  -- uncommenting this also makes the output appear
immediately */

    fprintf ( screen, "Enter script file name > " );
    fscanf ( screen, "%s", fred );

    fclose (screen);
}
T.RTitleUserPersonal
Name
DateLines
2145.1Being investigated at CRTL problem 1716TLE::D_SMITHDuane Smith -- DEC C RTLWed Apr 09 1997 08:553
    I have reproduced both the VAXC and DECC behavior that you have
    indicated.  The DECC behavior is reproducible in our current sources.
    We will investigate this problem.
2145.2workaroundTLE::D_SMITHDuane Smith -- DEC C RTLWed Apr 09 1997 09:1026
If the customer is looking for a workaround, it appears that replacing the 
fscanf with an fgets/sscanf combination works around the problem.


#include stdio
void main()
{
    FILE *screen;
    char buffer[100];
    char fred[100];

    screen = fopen ( "SYS$COMMAND", "r+" );

    fprintf ( screen, "Enter script file name > " );
    fgets (buffer, 100, screen);
    sscanf (buffer, "%s", fred);

/*    fflush( screen );  -- uncommenting this also makes the output appear
immediately */

    fprintf ( screen, "Enter script file name > " );
    fgets (buffer, 100, screen);
    sscanf (buffer, "%s", fred);

    fclose (screen);
}
2145.3AlwaysNQOS01::nyodialin20.nyo.dec.com::BowersDDave Bowers NSISWed Apr 09 1997 11:115
I've had so many problems over the years with scanf()/fscanf() (or my faulty 
understanding of its behavior) that I always use the gets(), sscanf() 
combination. It precludes a whole lot of problems.

\dave
2145.4Someone will always find it.COMICS::EDWARDSNDulce et decorum est pro PDP programWed Apr 09 1997 12:1111
As always, I am impressed at the speed and accuracy of replies.
The customer was wondering if there was anything dodgy about 
what he was up to and wasn't really sure that it was a bug.
If it is a bug, however, is there any need for it to be 
escalated formally?

I'll try to keep him at bay with the workaround for now.

Thanks again, muchly.

Neil.
2145.5TLE::D_SMITHDuane Smith -- DEC C RTLWed Apr 09 1997 12:529
    The problem report has been recorded and will be worked.  If the
    customer is satisfied with the workaround, then there is nothing
    more that you need to do.  If, on the other hand, the customer is
    not able to use the workaround and proceed, then I strongly encourage
    you to file an IPMT case.
    
    Either way, please thank the customer for bringing this to our
    attention.  This particular problem truly amazes me since it is 
    day one behavior.
2145.6SPECXN::DERAMODan D'EramoWed Apr 09 1997 13:5123
>    screen = fopen ( "SYS$COMMAND", "r+" );
>
>    fprintf ( screen, "Enter script file name > " );
>    fscanf ( screen, "%s", fred );
        
        From X3.159-1989 section 4.9.5.3 (The fopen Function) page 131
        
        	When a file is opened with update mode ('+' as the
        	second or third character in the above list of mode
        	argument values), both input and output may be
        	performed on the associated stream.  However, output
        	may not be directly followed by input without an
        	intervening call to the fflush function or to a file
        	positioning function (fseek, fsetpos, or rewind) and
        	input may not be directly followed by output without
        	an intervening call to a file position funciton,
        	unless the input operation encounters end-of-file.
        
        The above code violates that.  I'm not sure if we impose or
        document the above restriction though.  How does it work with
        the "required" sequence fprintf / fflush / fscanf?
        
        Dan
2145.7TLE::D_SMITHDuane Smith -- DEC C RTLWed Apr 09 1997 14:474
    > How does it work with
    >    the "required" sequence fprintf / fflush / fscanf?
    
    It doesn't.  Same behavior.
2145.8I'll raise it formally.COMICS::EDWARDSNDulce et decorum est pro PDP programThu Apr 10 1997 10:397
The customer has approx 2 million lines of code source to check through,
I would guess that searching through for all occurrences and changing 
them would be a real pain.

Stand by for an escalation.

Neil.
2145.9Perhaps the final update on this caseTLE::D_SMITHDuane Smith -- DEC C RTLWed May 28 1997 16:5036
[This update was sent from D_SMITH on 28-MAY-1997 15:49:36.38]
[CTG Support responsible for next action, Problem is undefined]

My apologies for taking so long to update this case.  What is happening is
the result of unconsumed characters remaining in the buffer after the fscanf
call is complete.  This unconsumed character is the terminator.  As suggested
earlier, the easiest workaround is to change the format specifier to "%s\n".

The behavior of the DEC C RTL is ANSI compliant.  I point to the following
paragraph contained in the ANSI description of the fopen function:

     When a file is opened with update mode ('+' as the second or third
     character in the above list of mode argument values), both input
     and output may be performed on the associated stream.  However,
     output may not be directly followed by input without an intervening
     call to the fflush function or to a file positioning function
     (fseek, fsetpos, or rewind) and input may not be directly followed
     by output without an intervening call to a file position function,
     unless the input operation encounters end-of-file.


The reason I did not provide this update earlier is that when SYS$COMMAND
points to a terminal device, the DEC C RTL does not allow file positioning
functions to succeed.  The behavior is being changed to allow positioning
to the beginning of the buffer using all of the above mentioned positioning
functions.  The easiest to add to the program is rewind().

After spending approximately 40 hours trying to change the DEC C RTL to
not require the code changes in the application, I have come up empty.
Please reconsider adding the "\n" to your format specifier.  If you end
up adding a positioning call, then your application will require that the
next round of ECO kits be installed to function correctly.  The change in
the format specifier would not require the ECO kit.


Duane Smith