Re: stdin ? stdin ? What's wrong with stdin? -- an UPDATE

From: Norm Dresner (ndrez_at_att.net)
Date: 02/17/05


Date: Thu, 17 Feb 2005 10:41:59 GMT

The project is a C++ Win32 Console App in VC++6. There are 12 .cpp-files
and 3 .h-files with ~3300 lines of code so I can't possibly present
everything here. As the program is starting up, it checks the command-line
for files to process which, for each of them, means reading the file,
producing an intermediate file which is closed and reopened for reading, and
this is read and the "output" file written. During my initialization, the
program checks to see if an output file exists for an input file and, if the
user hasn't already requested automatic deletion of pre-existing files, asks
the user if it's okay to delete an output file. The code snippet in that
module is

> > > if( DoesFileExist( FullOutputFileName ) )
> > > {
> > > if( !AutoDeleteOuputFiles )
> > > {
> > > printf( "-------- Output file exists? Delete it or skip
input file ([s]/d)? " );
> > > fgets( aux , sizeof( aux ) - 1 , stdin );
> > > }
> > > else
> > > ...

and everyone agrees that there's nothing wrong with the code as written, but
the program crashes with an access violation on the fgets() line. aux is
declared as char[1025] a few lines above and right-clicking in the debugger
just before the fgets() call is executed confirms this.

NEW INFORMATION: If I replace fgets( aux ... ) with gets( aux ) the program
runs perfectly!!!!! This means, at least to me, that there's really nothing
wrong with the "code" or with the "stdin" input stream because that's what
gets() uses. ALSO ... I started a brand-new console-app project which
contains just the following code:

    #include <libc.h /* this is my own */
                                /* include-just-about */
                                /* -everything-I'll-need */
                                /* header file that's also */
                                /* included in the */
                                /* previous program too */

        int main(int argc, char* argv[])
        {
        char aux[1025];
         printf("What's up doc? ");
         fgets( aux , sizeof( aux ) - 1 , stdin );
         printf( "\n\nYou entered \"%s\"\n" , aux );
        return 0;
        }

and this works just fine. SO ... There's obviously something in the way the
first program is setup -- a library or something -- that causes it to crash.
This is the only place in the program that stdin is used or any input from
the console is required. What should I be looking for? Could I possibly
be trashing some pointer somewhere? I don't quite see how if gets()
works because it looks like it calls getchar() which does fgetc() from
stdin itself.

Since I'm likely to be the only one using this program -- although a trusted
co-worker with equally high and obscure security clearance might be too
eventually -- should I just shrug my shoulders and leave the gets() call in
place or am I just deferring the problem until later?

Thanks
    Norm



Relevant Pages

  • Re: Help is needed to compile C program using Visual Studie 2005
    ... the pdb file that was used when this precompiled header was created, ... an output file whose name has the following format: ... The length of input file paths and name must be less than 256; ... while(i < DefinedVariableArrayIndex) { ...
    (microsoft.public.vc.language)
  • Re: Need advice on File I/O
    ... open the input file and open an output file, ... you would still have the input file unchanged. ... On all currently supported operating systems, ...
    (comp.soft-sys.matlab)
  • Re: Help with pattern matching
    ... then print the line to an output file. ... > replica of the input file. ... In Perl, CamelBack is generall reserved for package names. ... where their meaning must be expressed in comments. ...
    (perl.beginners)
  • Re: Help with pattern matching
    ... then print the line to an output file. ... > replica of the input file. ... If you had had warnings enabled as well as strict you might have found ... > # Assign the second field to an evaluation scalar ...
    (perl.beginners)
  • Re: Difficult text file to parse.
    ... > records are which there are only two, look at the output file below to ... I want to show the delimiters even if ... > My sample Input file: ... [sample input and output files with long fields snipped] ...
    (comp.lang.perl.misc)