Print everything after Ctr+D

This is a discussion on Print everything after Ctr+D within the C Programming forums, part of the General Programming Boards category; I want to create a small program that will get text with getchar() and print all the text added after ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    3

    Print everything after Ctr+D

    I want to create a small program that will get text with getchar() and print all the text added after the Ctr+D button
    Here is my code:

    Code:
    #include <stdio.h>
    
    main()
    {
    int c;
    while(c=getchar()) != EOF)
    if(c=EOF)
    {
    putchar(c);
    }
    
    }
    The problem is it doesn't working any ideas?

  2. #2
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int c;
    
        while ( (c=getchar()) != EOF )
        {
            putchar(c);
        }
    }

  3. #3
    old man
    Join Date
    Dec 2005
    Posts
    90
    Code:
    #include <stdio.h>
    
    #define B_SIZE 256
    
    int
    main (void)
    {
      char s[B_SIZE];  int i = 0, ch;
    
      while ((ch = getchar()) && i < B_SIZE-1)
      {
        if (ch == EOF)
          break;
        s[i++] = ch;
      }
      s[i] = '\0';
    
      printf ("\n\n\"%s\"\n\n", s);
      return 0;
    }

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    3
    In a strange way, noone of these program works?

  5. #5
    old man
    Join Date
    Dec 2005
    Posts
    90
    Please be specific. In what way doesn't mine work?

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    1
    Why would there be any text after the ctrl+D? You hit that to end input. The code looks for it and stops receiving input. If you want to keep receiving input you need to write code that ignores EOF, not that checks for it and stops when it gets it. You'll need some other way to trigger it to stop trying to get input though. Why do you want this anyway? The whole point of EOF is to say that there's nothing else after it.

  7. #7
    old man
    Join Date
    Dec 2005
    Posts
    90
    Quote Originally Posted by skiddieleet
    Why would there be any text after the ctrl+D?
    I assumed the OP wanted the text to be displayed after input had been closed with EOF -- ie, instead of being echoed one char at a time as input is collected with getchar() ... that way it makes sense.

    The only advantage to doing it this way rather than using the usual line-oriented methods is that input can include \n's ...

  8. #8
    Registered User
    Join Date
    Jan 2006
    Posts
    9
    Code:
    #include <stdio.h>
    main()
    {
    char c;
    while((c=getchar()) != EOF)
    ;
    if(c==(char)(-1))
    {
    putchar('d');
    }
    
    }
    check this out....its working....

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > if(c=EOF)
    1. It should be ==, not =
    2. It will never be true, the while loop test makes sure of that.

    Code:
    while ( (c=getchar()) != EOF ) {
      // do stuff upto first EOF
    }
    // ctrl-D gets you here, and more calls to getchar() will return EOF unless....
    clearerr( stdin );
    while ( (c=getchar()) != EOF ) {
      // do more stuff upto second EOF
    }

  10. #10
    Registered User
    Join Date
    Jan 2006
    Posts
    3
    kiranck007 not it isn't what I wanted to do

    I want a program that will let you write:
    for example
    asdjsajfhsdjhfjsdhfjksdhjfkhsdkjhfsdhf
    sdfsdhfjksdhjfkhsdjkahf
    asdfdsfsdf sdksjfsdfCOOL
    /*and after clicking Ctrl+D it will post to you everything you have writed*/
    asdjsajfhsdjhfjsdhfjksdhjfkhsdkjhfsdhf
    sdfsdhfjksdhjfkhsdjkahf
    asdfdsfsdf sdksjfsdfCOOL
    That's all!
    Code:
    #include <stdio.h>
    
    #define B_SIZE 256
    
    int
    main (void)
    {
              char s[B_SIZE];  int i = 0, ch;
    
                while ((ch = getchar()) && i < B_SIZE-1)
                          {
                                      if (ch == EOF)
                                                    break;
                                          s[i++] = ch;
                                            }
                  s[i] = '\0';
    
                    printf ("\n\n\"%s\"\n\n", s);
                      return 0;
    }
    I found this code but it is too complicated for me? Anyone know any better way?

  11. #11
    old man
    Join Date
    Dec 2005
    Posts
    90
    What on earth did you do to my formatting?

    Anyway, try this -- it does the same thing but it's simpler.

    Code:
    #include <stdio.h>
    
    int
    main (void)
    {
      char s[4096];  int i = 0, ch;
    
      while ((ch = getchar()) != EOF)
        s[i++] = ch;
      s[i] = '\0';
    
      printf ("\n\n%s\n\n", s);
      return 0;
    }

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    If you're using Windows, you need to use CTRL-Z, not CTRL-D.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. merging linked lists
    By scwizzo in forum C++ Programming
    Replies: 15
    Last Post: 09-14-2008, 05:07 PM
  2. storing alot of variables...
    By MikeyIckey in forum C Programming
    Replies: 11
    Last Post: 05-30-2008, 12:31 PM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  4. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 07:26 PM
  5. What kind of programs should I start writing?
    By Macabre in forum C++ Programming
    Replies: 23
    Last Post: 04-12-2003, 08:13 PM

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