fscanf() question

This is a discussion on fscanf() question within the C Programming forums, part of the General Programming Boards category; Hi, First my code: Code: #include <stdio.h> main() { FILE *stream; char dude[7]; int w; stream = fopen( "C:\bestand.dat", "r" ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    24

    fscanf() question

    Hi,

    First my code:

    Code:
    #include <stdio.h>
    
    main()
    {
          FILE *stream;
          char dude[7];
          int w;
          stream = fopen( "C:\\bestand.dat", "r" );
    
          while( !feof( stream ))
               {
               fscanf( stream, "%s", dude );
               printf("%s ", dude );
               }
    
          fclose( stream );
    
          getche();
    }
    bestand.dat contains the text
    Schrijven in een data bestand
    Now, my question,
    the word 'Schrijven' contains 10 characters \0 included.
    But i only declared dude[7], so basically 'Schrijven' wont fit into the variable dude.

    Is this because fscanf ( like scanf ) doenst check the buffer so an buffer overflow can occur?
    Just to make something clear


    Thanks for your time

    Encrypted

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Function fscanf() is one of those functions permitting overflows.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    24
    Ok man

    Thank You

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Graag gedaan.

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    24
    Cool,

    Nederlanders

    Laat ik erg nog een vraagje achter plakken:

    functies zoals getch, getche en getchar zijn toch allemaal wel ANSI-C?

    Dit omdat gcc de functie getch(e) niet herkent...

    English:

    Another question:

    functions like getch, getche en getchar are all ANSI-C right?

    this because gcc doesnt reconize them........

    Thanks again

  6. #6
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    No they r not standard functions.. They r usually available in he headr conio.h which is not standard either...

  7. #7
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Jazeker, Nederlanders. Er zijn er nog wel een paar, Travis Dane bijvoorbeeld.

    >functions like getch, getche en getchar are all ANSI-C right?

    Of those functions mentioned, only getchar() appears in my copy of the ANSI C standard. So I think the others are not standard. But there is another function, getc() which is.

    #include <stdio.h>
    int getc(FILE *stream);

    #include <stdio.h>
    int getchar(void);

    The getchar function is equivalent to getc with the argument stdin.
    Last edited by Shiro; 01-20-2003 at 12:20 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 08:31 PM
  2. Quick question about fscanf
    By zyphirr in forum C Programming
    Replies: 16
    Last Post: 11-24-2008, 08:44 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM
  5. quick fscanf question...
    By Ham in forum C++ Programming
    Replies: 4
    Last Post: 02-01-2003, 07:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21