ungetc question

This is a discussion on ungetc question within the C Programming forums, part of the General Programming Boards category; Can anyone tell me how to fix the code below so that the last char of my input file is ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    15

    ungetc question

    Can anyone tell me how to fix the code below so that the last char of my input file is not printed out twice? I know you might be wondering what I am doing..haha..but it is just the skeleton for a bigger program. Thanks..

    Code:
    int main(void)
    {
        char c;
        while( (c=getchar()) != EOF )
        {
          ungetc(c,stdin);
          printf("%c\n",c);
        }
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Code:
    int main(void)
    {
        char c;
        while( (c=getchar()) != EOF )
        {
    /* why is ungetc() here?  And, are you sure this causes the LAST character to print twice?
       Seems to me it should cause the FIRST character to print FOREVER */
    /*      ungetc(c,stdin);   */
          printf("%c\n",c);
        }
        return 0;
    }

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>char c;
    This is wrong. Read this
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 03:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 06:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 01:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 10:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM

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