Unusual Input with fgets( )

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Newbie

    #1

    Unusual Input with fgets( )

    Hello I need to enter a string of the form abc[EOF] (a string of
    characters followed by EOF)

    #include<stdio. h>
    #include<stdlib .h>
    #include<string .h>
    #define MAX 100

    int main(void)
    {
    char arr[MAX];
    puts("Enter String :");

    fgets(arra,100, stdin);

    /*Do Something*/
    puts("");
    return 0;
    }

    Is there any way i can do this ?The code i pasted above halts once an
    EOF is entered.Is there any way to prevent the program from halting for
    any further input ?

    Actually this is a part of a bigger question where i am trying to parse
    a string ( to validate user entered int/double etc )
    A Simple EOF is easy to handle but if the EOF is entered after a set of
    characters the program waits for a further input!!

    What is the right way to handle such an input ?
  • Keith Thompson

    #2
    Re: Unusual Input with fgets( )

    Newbie <newbie@gmail.c omwrites:
    Hello I need to enter a string of the form abc[EOF] (a string of
    characters followed by EOF)
    I assume you mean end-of-file, not the characters 'E', 'O', 'F'.

    The characters 'a', 'b', 'c' can of course be contained in a string.
    EOF can't. EOF is a macro that expands to a constant expression with
    a negative value (typically -1, but don't depend on that). It's a
    value returned by functions like getchar() to indicate an end-of-file
    condition. It's not a character value. If you try to store the value
    EOF in a string, you can do it, but it will be converted from type int
    to type char and will give you an ordinary character value (on most
    systems that use an ASCII-derived character set, it's a lowercase 'y'
    with two dots over it, what Unicode calls "LATIN SMALL LETTER Y WITH
    DIAERESIS").
    #include<stdio. h>
    #include<stdlib .h>
    #include<string .h>
    #define MAX 100
    >
    int main(void)
    {
    char arr[MAX];
    puts("Enter String :");
    >
    fgets(arra,100, stdin);
    You declared MAX; you should use it here:

    fgets(arra, MAX, stdin);
    /*Do Something*/
    puts("");
    return 0;
    }
    >
    Is there any way i can do this ?The code i pasted above halts once an
    EOF is entered.Is there any way to prevent the program from halting
    for any further input ?
    >
    Actually this is a part of a bigger question where i am trying to
    parse a string ( to validate user entered int/double etc )
    A Simple EOF is easy to handle but if the EOF is entered after a set
    of characters the program waits for a further input!!
    >
    What is the right way to handle such an input ?
    It's hard to tell why your program halts, since you haven't shown us
    the whole thing. The program you posted halts after attempting to
    read a single line -- at least, it does once you fix the typo ("arra"
    should be "arr"). It's always best to copy-and-paste your actual
    code; if you post some approximation of it, it's impossible to tell
    which problems are in your real code and which were introduced when
    you summarized it.

    For keyboard input, end-of-file is usually indicated by pressing some
    system-specific sequence, typically control-D (for Unix-like systems)
    or control-Z (for DOS/Windows-like systems). This is intended to
    indicate that you've reached the end of your input, so trying to read
    more input after that doesn't generally make much sense.

    The actual behavior for interactive devices varies across systems, and
    the intent of the standard is (arguably) unclear -- which emphasizes
    the point that you should avoid depending on it.

    Since end-of-file is a condition, different input functions respond to
    it in different ways (getchar() returns EOF, fgets() returns a null
    pointer, etc.). Consult the documentation for each function you want
    to use (always a good idea regardless).

    If you insist on trying to read input after reaching end-of-file, you
    can call clearerr(stdin) . Assuming this works, it means your program
    will work differently reading from a file (where the end-of-file
    condition will be set the next time you try to read from it, since
    there just isn't anything more to read) than from an interactive
    device such a keyboard.

    It's better to design your program so that end-of-file doesn't occur
    until you're actually done.

    See also section 12 of the comp.lang.c FAQ, <http://www.c-faq.com/>.

    --
    Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

    Comment

    • Newbie

      #3
      Re: Unusual Input with fgets( )

      Keith Thompson wrote:
      Newbie <newbie@gmail.c omwrites:
      >Hello I need to enter a string of the form abc[EOF] (a string of
      >characters followed by EOF)
      >
      I assume you mean end-of-file, not the characters 'E', 'O', 'F'.
      >
      The characters 'a', 'b', 'c' can of course be contained in a string.
      EOF can't. EOF is a macro that expands to a constant expression with
      a negative value (typically -1, but don't depend on that). It's a
      value returned by functions like getchar() to indicate an end-of-file
      condition. It's not a character value. If you try to store the value
      EOF in a string, you can do it, but it will be converted from type int
      to type char and will give you an ordinary character value (on most
      systems that use an ASCII-derived character set, it's a lowercase 'y'
      with two dots over it, what Unicode calls "LATIN SMALL LETTER Y WITH
      DIAERESIS").
      >
      Yeah ,I know this!
      ...snip...
      >What is the right way to handle such an input ?
      >
      It's hard to tell why your program halts, since you haven't shown us
      the whole thing. The program you posted halts after attempting to
      read a single line -- at least, it does once you fix the typo ("arra"
      should be "arr"). It's always best to copy-and-paste your actual
      code; if you post some approximation of it, it's impossible to tell
      which problems are in your real code and which were introduced when
      you summarized it.
      #include<stdio. h>
      #include<stdlib .h>
      #include<string .h>
      #define MAX 100

      int main(void)
      {
      char arr[MAX];
      printf("Enter The String :");

      if( fgets(arr,MAX,s tdin) == NULL )
      {
      fprintf(stderr, "Bad Input");
      return 1;
      }
      puts("");
      return 0;
      }

      Basically i need the user to enter a string to be parsed later on in the
      program. If the user enters a simple EOF (ctrl z or ctrl d as the system
      may be, it is easy to detect since fgets will return NULL and feof will
      detect the EOF in the input stream)

      I am facing the problem if i have an input of the form :
      (Say on Windows):

      abc^Z(enter key)
      or
      abc^Z(enter key)

      In each of the two cases above i need to press a key again to continue
      with the next line of the program .fgets does not return anything till i
      press a key again.

      So the input *becomes*

      abc^Z(enter key)
      (enter key)

      /*further processing*/

      I cannot control what a user enters when he is asked to. I can process
      the data later on but how do i handle an input like this at runtime?
      For keyboard input, end-of-file is usually indicated by pressing some
      system-specific sequence, typically control-D (for Unix-like systems)
      or control-Z (for DOS/Windows-like systems). This is intended to
      indicate that you've reached the end of your input, so trying to read
      more input after that doesn't generally make much sense.
      >
      The actual behavior for interactive devices varies across systems, and
      the intent of the standard is (arguably) unclear -- which emphasizes
      the point that you should avoid depending on it.
      >
      Since end-of-file is a condition, different input functions respond to
      it in different ways (getchar() returns EOF, fgets() returns a null
      pointer, etc.). Consult the documentation for each function you want
      to use (always a good idea regardless).
      >
      If you insist on trying to read input after reaching end-of-file, you
      can call clearerr(stdin) . Assuming this works, it means your program
      will work differently reading from a file (where the end-of-file
      condition will be set the next time you try to read from it, since
      there just isn't anything more to read) than from an interactive
      device such a keyboard.
      >
      It's better to design your program so that end-of-file doesn't occur
      until you're actually done.
      >
      See also section 12 of the comp.lang.c FAQ, <http://www.c-faq.com/>.
      >

      Comment

      • Newbie

        #4
        Re: Unusual Input with fgets( )

        Newbie wrote:

        ...snip...
        I am facing the problem if i have an input of the form :
        (Say on Windows):
        >
        abc^Z(enter key)
        or
        abc^Z(enter key)
        This should read as abc^Zefg(enter key)

        Comment

        • Keith Thompson

          #5
          Re: Unusual Input with fgets( )

          Newbie <newbie@gmail.c omwrites:
          Keith Thompson wrote:
          >Newbie <newbie@gmail.c omwrites:
          >>Hello I need to enter a string of the form abc[EOF] (a string of
          >>characters followed by EOF)
          >I assume you mean end-of-file, not the characters 'E', 'O', 'F'.
          >The characters 'a', 'b', 'c' can of course be contained in a string.
          >EOF can't. EOF is a macro that expands to a constant expression with
          >a negative value (typically -1, but don't depend on that). It's a
          >value returned by functions like getchar() to indicate an end-of-file
          >condition. It's not a character value. If you try to store the value
          >EOF in a string, you can do it, but it will be converted from type int
          >to type char and will give you an ordinary character value (on most
          >systems that use an ASCII-derived character set, it's a lowercase 'y'
          >with two dots over it, what Unicode calls "LATIN SMALL LETTER Y WITH
          >DIAERESIS").
          >>
          >
          Yeah ,I know this!
          ..snip...
          [more snippage]
          Basically i need the user to enter a string to be parsed later on in
          the program. If the user enters a simple EOF (ctrl z or ctrl d as the
          system may be, it is easy to detect since fgets will return NULL and
          feof will detect the EOF in the input stream)
          Right.
          I am facing the problem if i have an input of the form :
          (Say on Windows):
          >
          abc^Z(enter key)
          or
          abc^Z(enter key)
          >
          In each of the two cases above i need to press a key again to continue
          with the next line of the program .fgets does not return anything till
          i press a key again.
          >
          So the input *becomes*
          >
          abc^Z(enter key)
          (enter key)
          Right, because that's what the user entered.

          Apparently windows uses control-Z at the beginning of a line to
          trigger an end-of-file condition. Control-Z *not* at the beginning of
          a line is just another control character, not treated specially. In
          that context, it's no different than control-X or control-Y, and you
          should probably treat it the same way.
          /*further processing*/
          >
          I cannot control what a user enters when he is asked to. I can process
          the data later on but how do i handle an input like this at runtime?
          [...]

          If you like, you can scan the input string for a control-Z character:

          #define CONTROL_Z '\x1a'

          How your program should react to that depends on your requirements.

          Or you can check for *any* non-printable character, perhaps using
          isprint() (don't forget to convert the argument to unsigned char).
          Again, how you handle such input is up to you.

          --
          Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
          Nokia
          "We must do something. This is something. Therefore, we must do this."
          -- Antony Jay and Jonathan Lynn, "Yes Minister"

          Comment

          Working...